Thread: C++ Casting (re-interpret cast)

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by Davros
    >Not exactly. Think of them on a level of safety..start with static_cast...if that doesnt work then your likely doing something a little dangerous

    When you say a 'level of safety', do you mean I will get compiler warnings rather than any runtime change in behaviour?.
    I just mean common sense. Casting say an int to a pointer can be dangerous if you dont know what you are doing IE access violation.

  2. #17
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Now you're losing me there and I thought I nearly had it.

    I know that this is bad:

    int a = 5;
    MYCLASS* b = (MYCLASS*)a;
    b->printSomething();

    If I used either reinterpret_cast or static_cast to cast a to b', would it give something (anything) over the above C style example? Would either of them warn me or fail to compile.

    I'm simply trying work out whether reinterpret or static do anything different than a C style cast and I thought I had there a minute ago. But I'm, confused again now.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #18
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A static_cast would fail (compile time) there. A reinterpret_cast would let you shoot yourself in the foot.

  4. #19
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >A static_cast would fail (compile time) there.

    OK. I think I've got. reinterpret_cast behaves the same as C style casts, while static_cast and const_cast offer some level of type safety.

    If this is right, thanks for all the help.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  5. #20
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Fordy
    static_cast is a pretty safe little cast for converting things like ints to doubles etc...casts that wont cause any hassle

    const_cast casts away the const quality of a variable and is handy in some tight circumstances when you know what you are doing

    reinterpret_cast is the cast you use when you are doing something potentially dangerous & out of the ordinary that you are forced to do like casting a pointer to a differnt pointer or (more likely for winapi) casting an intergal to a function pointer (IE LONG to a WNDPROC)
    Perhaps I can elaborate a little:

    const_cast is used to convert from a pointer/reference to a constant type to a pointer/reference to the same type but without the const modifier

    Here's how it'd look if const_cast wasn't a keyword:
    Code:
    template<typename T>
    T* const_cast( const T* ptr)
    {
      return (T*)ptr;  //C-style cast
    }
    //The same for references
    Const cast might result in undefined behaviour.

    Const cast must be used on pointer/reference types:
    Code:
    const int i = 0;
    const_cast<int>(i) = 23;  //Compile error
    const_cast<int&>(i) = 23; //Legal, but undefined
    *(const_cast<int*>(&i)) = 23; //Legal, but undefined
    static_cast is used to convert between:
    • Numerical types
    • Pointers/references within the same class hierarchy without any run-time checks. If used so, it's a dynamic_cast that always succeed.
    Code:
    struct A { };
    struct B : A { };
    struct C { };
    int main()
    {
    	A* a;
    	B* b;
    	C* c;
    	b = a;				  //Compiler error
    	b = static_cast<B*>(a); //Can be dangerous
    	c = b;				  //Complier error
    	c = static_cast<C*>(b); //Compiler error -- C is unrelated to B
    }
    reinterpret_cast converts between totally unrelated types, for example between long and int*. It can also perform the conversion above, where static_cast will fail.

    Hope that helps, distingushing between these cast are not always trivial.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #21
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Davros
    OK. I think I've got. reinterpret_cast behaves the same as C style casts.
    No, reinterpret_cast cannot perform everything that a C-style cast can:

    Code:
    const int i = 0;
     
    *((int*)&i)			 = 23; //C-style cast	 -- succeed
    *const_cast<int*>(&i)		 = 23; //const_cast		-- succeed
    *reinterpret_cast<int*>(&i)	= 23; //reinterpret_cast -- FAILED

    Last edited by Sang-drax; 07-20-2004 at 12:49 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #22
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks Sang-drax. Your explanation was so good, I'll copy it and save it for reference.

    Cheers
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM