wtf is reinterrpret_cast
wtf is reinterrpret_cast
today I had a lecture on c++. I am confused with the diferrence between a stardard cast and a reinterrpret_cast.
Re:wtf is reinterrpret_cast
C style cast:
The problem with that is that it casts foo to mytype_t, period. Several things can go wrong here, and the compiler can't really complain because it has been told it should do this - the code might be syntactically correct, but all kinds of bugs can be hidden by a bad cast. And you can't even grep for it efficiently...
C++ introduces four different types of cast:
reinterpret_cast, to answer your question, takes a pointer to something (let's say, a void *), and tells the compiler to handle the memory pointed to as something completely different, let's say a mytype_t *.
Needless to say that, if the memory pointed to is something else but a mytype_t, you'll be in trouble shortly.
Effectively, reinterpret_cast is equivalent to C-style casting, and you should avoid it if at all possible.
Edit: http://www.cplusplus.com/doc/tutorial/typecasting.html
Code: Select all
(mytype_t)foo;
C++ introduces four different types of cast:
- const_cast - add or remove "const" qualifier
- dynamic_cast - casting class pointers up or down the inheritance graph, including proper type-checking
- static_cast - like dynamic cast, but without type checking
- reinterpret_cast - cast between pointers of unrelated classes
reinterpret_cast, to answer your question, takes a pointer to something (let's say, a void *), and tells the compiler to handle the memory pointed to as something completely different, let's say a mytype_t *.
Needless to say that, if the memory pointed to is something else but a mytype_t, you'll be in trouble shortly.
Effectively, reinterpret_cast is equivalent to C-style casting, and you should avoid it if at all possible.
Edit: http://www.cplusplus.com/doc/tutorial/typecasting.html
Every good solution is obvious once you've found it.
Re:wtf is reinterrpret_cast
The information above is essentially correct, but I don't think I'd describe reinterpret_cast as "C-style casting". C-style casting is simply casting with the type name in parentheses, and in fact one of the problems with it is that it does all of the C++ casts except dynamic_cast.
Of particular note is that doing a static_cast from void* to mytype_t* isn't really any different than doing a reinterpret_cast between the two. Where the difference lies is that reinterpret_cast allows you to cast directly from something like a double* to an int* -- i.e., pointers between unrelated types. You can achieve the same result with static_cast, but you first have to cast to void* and then cast to the unrelated type. So:
Of particular note is that doing a static_cast from void* to mytype_t* isn't really any different than doing a reinterpret_cast between the two. Where the difference lies is that reinterpret_cast allows you to cast directly from something like a double* to an int* -- i.e., pointers between unrelated types. You can achieve the same result with static_cast, but you first have to cast to void* and then cast to the unrelated type. So:
Code: Select all
double d = 2523.2342;
// the following will give an error
int* p1 = static_cast<int*>(&d);
// the following two lines effectivey do the same thing
int* p2 = static_cast<int*>(static_cast<void*>(&d));
int* p3 = reinterpret_cast<int*>(&d);
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:wtf is reinterrpret_cast
Also, C-style casts can sometimes mean reinterpret_cast when you want them to mean static_cast. I found a bug once where a C-style downcast in the presence of multiple inheritence produced an invalid pointer.
Bottom line: Avoid C-style casts in C++ as a matter of habit.
Bottom line: Avoid C-style casts in C++ as a matter of habit.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:wtf is reinterrpret_cast
In the meaning of, does it no matter whether it makes sense or not.Joel wrote: The information above is essentially correct, but I don't think I'd describe reinterpret_cast as "C-style casting".
Every good solution is obvious once you've found it.