468,766 Members | 1,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

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 5180

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tanmoy Bhattacharya | last post: by
2 posts views Thread by Dave | last post: by
2 posts views Thread by Alex Sedow | last post: by
31 posts views Thread by Michael C | last post: by
12 posts views Thread by Rahul | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.