Implicit/explicit conversion

Discussion in 'C++' started by al, Jan 5, 2004.

  1. al

    al Guest

    1. "Implicit conversion happens in three situations: expression, passing in
    arguments of functions and returning expression from function." What is
    "returning expression from function" here?

    2. This is what my learning about conversion in C++:
    Generally in C++, casting should be avoided.
    Use explicit conversion rather than implicit conversion.
    Use static_cast rather than dynamic_cast or reinterpret_cast, especially
    when such conversion is safe during coding.
    Use dynamic_cast with casting in a class hierarchy.

    Is the above understanding correct conceptually?

    3. Is the reason that reinterpret_cast should be avoided that it is system
    dependent? What is system dependent here regarding reinterpret_cast? Is
    there any other reason?

    4. "The static_cast is available is available for a conversion that is
    portable, well-defined, and invertible." Could someone help me understand
    this statement better?

    Thanks!
     
    al, Jan 5, 2004
    #1
    1. Advertising

  2. "al" <> wrote...
    > 1. "Implicit conversion happens in three situations: expression, passing

    in
    > arguments of functions and returning expression from function." What is
    > "returning expression from function" here?


    double foo() {
    return 42;
    }

    The expression '42' here is an int. It will be implicitly converted
    to double during function return.

    >
    > 2. This is what my learning about conversion in C++:
    > Generally in C++, casting should be avoided.
    > Use explicit conversion rather than implicit conversion.
    > Use static_cast rather than dynamic_cast or reinterpret_cast, especially
    > when such conversion is safe during coding.
    > Use dynamic_cast with casting in a class hierarchy.
    >
    > Is the above understanding correct conceptually?


    I cannot claim full understanding why you have "use explicit rather
    than implicit" there. 'static_cast' is a compile-time conversion.
    In a class hierarchy, derived is converted to base implicitly (often,
    if possible, anyway), using 'dynamic_cast' for that is overkill. I
    don't know if this is something you expected as an answer.

    >
    > 3. Is the reason that reinterpret_cast should be avoided that it is system
    > dependent? What is system dependent here regarding reinterpret_cast? Is
    > there any other reason?


    The Standard says that reinterpret_cast is only guaranteed to work for
    conversion A->B->A, and everything else may not work (implementation-
    defined).

    >
    > 4. "The static_cast is available is available for a conversion that is
    > portable, well-defined, and invertible." Could someone help me understand
    > this statement better?


    Where did you find it? Is appearance of "is available" twice a typo?

    Usually static_cast is used for a conversion T1->T2 if T2->T1 is implicit.
    That's "invertible" part. What they mean by well-defined or portable, I
    don't know. FWIW, every C++ program is portable unless it uses some kind
    of implementation-defined properties (and it still may be portable, just
    not guaranteed to be).

    Victor
     
    Victor Bazarov, Jan 5, 2004
    #2
    1. Advertising

  3. > > 4. "The static_cast is available is available for a conversion that is
    > > portable, well-defined, and invertible." Could someone help me

    understand
    > > this statement better?

    >
    > Where did you find it? Is appearance of "is available" twice a typo?


    I also wondered once. I guess it was in the Stroustroup.

    Greetings
    Ernst
     
    Ernst Murnleitner, Jan 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dmoos AT esigma-systems DOT de

    implicit vs. explicit type conversion for string vs. (char *)

    dmoos AT esigma-systems DOT de, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    2,556
    Default User
    Jun 26, 2003
  2. iceColdFire

    explicit Vs Implicit conversions?

    iceColdFire, May 9, 2005, in forum: C++
    Replies:
    1
    Views:
    5,435
    Victor Bazarov
    May 9, 2005
  3. RT Lange
    Replies:
    6
    Views:
    299
    Shalabh Chaturvedi
    Dec 11, 2003
  4. /* frank */

    Implicit and explicit conversion

    /* frank */, Jul 3, 2004, in forum: C Programming
    Replies:
    3
    Views:
    402
    Dan Pop
    Jul 5, 2004
  5. Pierre Yves
    Replies:
    3
    Views:
    482
    Pierre Yves
    Nov 1, 2007
Loading...

Share This Page