Page 1 of 1
wtf is reinterrpret_cast
Posted: Wed Feb 08, 2006 1:03 am
by B.E
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
Posted: Wed Feb 08, 2006 1:26 am
by Solar
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:
- 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
The advantage is that the compiler has some idea of what you're trying to do with the cast, and can tell you if you're doing it wrongly. It's also easy to grep for them.
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
Re:wtf is reinterrpret_cast
Posted: Wed Feb 08, 2006 1:37 am
by B.E
it makes sence now thanks solar
Re:wtf is reinterrpret_cast
Posted: Wed Feb 08, 2006 10:13 am
by ark
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:
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);
Re:wtf is reinterrpret_cast
Posted: Wed Feb 08, 2006 12:57 pm
by Colonel Kernel
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.
Re:wtf is reinterrpret_cast
Posted: Wed Feb 08, 2006 2:49 pm
by Solar
Joel wrote:
The information above is essentially correct, but I don't think I'd describe reinterpret_cast as "C-style casting".
In the meaning of, does it no matter whether it makes sense or not.