Type Casting

This is a discussion on Type Casting within the C++ Programming forums, part of the General Programming Boards category; I recently purchased the book C++ for C Programmers, 3rd Ed. by Ira Pohl. It was a $40 book for ...

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Question Type Casting

    I recently purchased the book C++ for C Programmers, 3rd Ed. by Ira Pohl. It was a $40 book for only $10. It's telling me that instead of something like
    Code:
    int i = 3;
    cout << 2/double(i);
    I should use the keyword static_cast like so
    Code:
    int i = 3;
    cout << 2/static_cast<double>(i);
    I don't really understand why this is. Of course, I'm just judging this on the fact that the latter method is longer to type. Please tell me why the latter method is perferable. Thanks.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    It is a part of the new standard C++ set fourth by ANSI and ISO. Also you can use reinterpret_cast<> as well. They are prefered than doing the type() stye of C. Just a "Cleaner" way of doing it. It also allows the compiler to do type checking.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by velius
    Also you can use reinterpret_cast<> as well.
    My book really just glosses over this issue of casting. What makes reinterpret_cast different from static_cast?
    They are prefered than doing the type() stye of C. Just a "Cleaner" way of doing it. It also allows the compiler to do type checking.
    How is it "cleaner"? And I don't understand what you mean by that last sentence. How was the language lacking before these new reserved words? I didn't know of a way to screw up a program by casting to an undefined type. I thought that an error like that would be caught at compile time.

    Thanks for your help in advance.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    A static_cast converts between related types, for instance int to float. A reinterpret_cast converts between to unrelated types, for instance an integral type to a pointer.

    There is also a dynamic_cast, which allows you to cast from a polymorphic virtual base class to a derived class.

    How is it "cleaner"? And I don't understand what you mean by that last sentence. How was the language lacking before these new reserved words? I didn't know of a way to screw up a program by casting to an undefined type. I thought that an error like that would be caught at compile time.
    The old C style cast operator could obscure some errors, particularly when you are dealing with polymorphism. The new keywords are also more expressive. You have a much better idea of what the programmer intended when they used the cast. I guess it also fits into C++'s stronger typechecking paradigm too.
    Last edited by foniks munkee; 09-06-2003 at 08:18 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There is also a const_cast which casts away the constness of a variable.

    Avoid reinterpret_cast if at all possible. Essentially, it just takes the bit pattern of the given variable and interprets that bit pattern as the type specified. Very bad in terms of type-correctness, and can be avoided most of the time.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thank you for all of your help thus far. I have another question. I know how to overload an operator for explicit type casting like double(fraction &f). Now, with this new way of casting, how do I do that?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    When you do a static_cast, the compiler will search for the conversion operator (defined as normal) in your class to that particular type. The other casts are all defined in such a way that it would make no sense to be able to overload them.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21