472,962 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,962 software developers and data experts.

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 5578

"Stub" <st**@asof.com> wrote in message
news:k7*********************@bgtnsc05-news.ops.worldnet.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<ExClass>(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
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...
2
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...
3
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 {...
4
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...
2
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...
31
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();...
1
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....
42
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. ...
12
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? ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.