Which one to use when static

Possible Duplicate:
Should I use static_cast or reinterpret_cast when casting a void* to whatever

Often, especially in Win32 programming it is required to cast from one opaque type to another. For example:

 HFONT font = cast_here<HFONT>( ::GetStockObject( SYSTEM_FONT ) );

Both static_cast and reinterpret_cast are applicable here and have exactly the same effect since HFONT is a pointer to a dummy struct specifically introduced for defining HFONT and HGDIOBJ returned by GetStockObject() is a void* pointer.

Which one - static_cast or reinterpret_cast - is preferable?


Everybody has noted that reinterpret_cast<> is more dangerous than static_cast<>.
This is because reinterpret_cast<> ignores all type information and just assigns a new type without any real processing, as a result the processing done is implementation defined (though usually the bit patterns of the pointers are the same).

The thing everybody fails to mention is that reinterpret_cast<> is a means to document your program. It tells somebody reading the code that we had to compromise something and as a result we have ended up with a dangerous cast, and be careful when you mess with this code.

Use the reinterpret_cast<> to highlight these dangerous areas in the code.

When casting from a void* there is not type information for the cast to work with.
So you are either doing an invalid cast or a casting back to the original type that was previously cast into a void*. Any other type of casting is going to end up with some undefined behavior.

This is the perfect situation to use reinterpret_cast<> as the standard guarantees that casting a pointer to void* and back to its original type using reinterpret_cast<> will work. And by using reinterpret_cast<> you are pointing out to the humans that come along afterwords that something bad is happening here.


It's not correct to state the the casts will have the same affect. The casts do two completely different things:

  • static_cast<T>(x) says convert expression x to type T .
  • reinterpret_cast<T*>(&x) says interpret the memory location '&x' as a T*
  • Consider the following:

    struct A1 { int a1; };
    struct A2 { int a2; };
    struct B : public A1, public A2 {};
    
    void foo (A1 * a1, A2 * a2)
    {
      B * b1_a1 = static_cast<B*> (a1);
      B * b2_a1 = reinterpret_cast<B*> (a1);
      B * b1_a2 = static_cast<B*> (a2);
      B * b2_a2 = reinterpret_cast<B*> (a2);
    
      std::cout << "b1_a1==b1_a2" << (b1_a1==b1_a2) << std::endl;
      std::cout << "b2_a1==b2_a2" << (b2_a1==b2_a2) << std::endl;
    }
    
    int main ()
    {
      B b;
      foo (&b, &b);
    }
    

    This program results in the following output:

    g++ -o t t.cc ; ./t
    b1_a1==b1_a2: 1
    b2_a1==b2_a2: 0
    

    This shows how the static_cast from a2 to b2 correctly adjusted the pointer so that it points to the beginning of b , but the reinterpret_cast did not.


    static_cast is always preferable, avoid doing reinterpret_cast s unless absolutely neccessary.

    reinterpret_cast is the least typesafe cast.

    More information at this website about which casts should be used when.

    链接地址: http://www.djcxy.com/p/28748.html

    上一篇: C ++何时应该更喜欢使用两个链式静态

    下一篇: 静态时使用哪一个