Re: Explicit conversion

From: Michael Kochetkov (Michael.Kochetkov_at_synartra.commmm)
Date: 11/12/03


Date: Thu, 13 Nov 2003 00:05:08 +0300


"Stub" <stub@asof.com> wrote in message
news:k7vsb.52956$Ec1.3404724@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.


Relevant Pages

  • Re: explicit conversion
    ... > How are you going to make the compiler distinguish between 'Type2 ... introduction of 'explicit' type conversion ... constructor is invoked as Type2::Type2), ...
    (comp.lang.cpp)
  • Explicit conversion
    ... Docs says that "The compiler does not use an explicit constructor to ... implement an implied conversion of types. ... struct ExClass{ ...
    (comp.lang.cpp)
  • Re: inserting textbox value into bookmark
    ... What is happening if you omit the CStr function is that an implicit type ... because the implicit type conversion does what I expect. ... I'm not laying this down as a law - "Thou shalt use explicit type ...
    (microsoft.public.word.vba.general)
  • Re: On "type casting"
    ... whether used in reference to an implicit conversion or an explicit ... refer to an explicit conversion is considered wrong by a number of people on this newsgroup who think type casting is only what ... They believe "cast" is correct but "type cast" isn't. ...
    (comp.lang.c)
  • Re: More static type fun.
    ... languages, such a conversion must be explicit, though there are a few ... a type is always explicit though, ... This is different to the situation in dynamically typed languages ... difference between static typing with a union-of-all-types-type ...
    (comp.lang.lisp)