473,757 Members | 10,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explicit conversion

Docs says that "The compiler does not use an explicit constructor to
implement an implied conversion of types. It's purpose is reserved
explicitly for construction."

I put up code of three cases at the bottom. Hope you can help me understand
the "explicit" keyword and its usage. Specifically,

Is "explicit" keyword only associated with constructor in C++?

What's "implied conversion of types"?

Is "ExClass Ex=5;" so-called implied conversion since it converts an int
into ExClass type? If so then why "ExClass Ex(5);" is not since it calls a
conversion constructor?
Thank you for your help!
Case 1:
---------------
struct ExClass{
explicit ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex=5;
return 0;
}

OUTPUT:
implicit conversion
Case 2:
----------------
struct ExClass{
ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex=5;
return 0;
}

OUTPUT:
explicit conversion
Case 3:
-------------------
struct ExClass{
ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex(5);
return 0;
}

OUTPUT:
explicit conversion
Jul 19 '05 #1
1 5654

"Stub" <st**@asof.co m> wrote in message
news:k7******** *************@b gtnsc05-news.ops.worldn et.att.net...
Docs says that "The compiler does not use an explicit constructor to
implement an implied conversion of types. It's purpose is reserved
explicitly for construction."

I put up code of three cases at the bottom. Hope you can help me understand the "explicit" keyword and its usage. Specifically,

Is "explicit" keyword only associated with constructor in C++? I would read it as "... with a constructor in C++?" if you do not mind. Then
no, the "explicit" keyword deals with constructors declarations only withing
class declarations.

What's "implied conversion of types"? Let us say, an object "a" of type A may be implicitly converted to a type B
if and only if the B b = a; declaration exists.

Is "ExClass Ex=5;" so-called implied conversion since it converts an int
into ExClass type? If you do not mind the definition above then it is so by definition.
If so then why "ExClass Ex(5);" is not since it calls a
conversion constructor? The expression above is the direct initialization. Direct initializations
are a kind of explicit initialization.


Thank you for your help!
Case 1:
---------------
struct ExClass{
explicit ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex=5;
return 0;
}

OUTPUT:
implicit conversion Yes, it is. It is so by definition. I see, that your compiler is not an
Intel-made one. Though it looks like other EDG-base compilers can handle it
right.


Case 2:
----------------
struct ExClass{
ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex=5;
return 0;
}

OUTPUT:
explicit conversion ExClass is the best conversion as far as 5 is of type int.


Case 3:
-------------------
struct ExClass{
ExClass(int) { cout << "explicit conversion"; }
ExClass(long) { cout << "implicit conversion"; }
};

int main (){
ExClass Ex(5);
return 0;
}

OUTPUT:
explicit conversion

Direct initialization is the explicit conversion indeed.
Other explicit conversions are:
explicit cast conversion in cast notation: ExClass Ex = (ExClass)5;
static_cast expression: ExClass Ex = static_cast<ExC lass>(5);

I cannot think out the explicit cast conversion in functional notation -- it
constantly leads my to direct initialization. But it might be applicable
too.

--
Michael Kochetkov.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2373
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want people to be able to initialize real numbers (like an object of type double) from it ... in such a context (or if someone explicitly casts to double), the imaginary part is to be ignored. However, I certainly do not want people to be able to ask...
2
2146
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the possibility of an implicit conversion from type T to type T? Such an implicit conversion could never occur because you're already of the required type! Thanks,
3
2111
by: spamadress | last post by:
Hello I wonder why only constructors can be qualified with explicit to prevent implicit conversion, but not conversion operators. To me the following makes perfectly sense: struct A { explicit operator int() { return 0;} ; }; void foo(int) {};
4
2662
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO lay out what should happen for: -------------------------- signed char sc; unsigned char uc;
2
6867
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public static explicit System.IntPtr (long);
31
3616
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
1
2323
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for call to 'D<int>::D(D<int>&)'
42
2697
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. Well, I came across Scott Meyer's SmartPtr example from some 10 years ago. I like the template member function for type conversion to solve inheritance issues. On MSVS 2003, I get the below error, if I declare the constructor SmartPtr(T*...
12
7210
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.