473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 5613

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.