reinterpret_cast examples?

Started by
39 comments, last by Brother Bob 10 years, 11 months ago

I've been reading a little bit about reinterpret_cast,and I have a problem thinking about when I could really use it.I can use it to cast a pointer to a totally unreleated type of pointer.

Are there some good examples to show some situations you can't solve without reinterpret_cast?

Advertisement

Well, a C-style cast trumps everything (can do all types of casts).

One use is to get the binary representation of a float, reinterpret_cast the address to an int*, so you can do bit-twiddling operations on it (like clearing or setting the sign bit, stuff like that).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I've been reading a little bit about reinterpret_cast,and I have a problem thinking about when I could really use it.I can use it to cast a pointer to a totally unreleated type of pointer.

reinterpret_cast is for casting between unrelated types, so yes, you can use it to cast a pointer of one type to a pointer of another unrelated type.

Are there some good examples to show some situations you can't solve without reinterpret_cast?

Well, casting between unrelated pointer types requires a cast, and a reinterpret_cast is the cast for that.

Well, a C-style cast trumps everything (can do all types of casts).

That is actually not true. It cannot do casts of pointer to classes with multiple inheritance in some cases. The C-style cast will just reinterpret the pointer value, but a proper C++ style cast may actually change the value to handle multiple inheritance correctly.

Well, you learn something new every day ;)

What used to happen before the new C++ casts? They're templated and were templates introduced at the same time as multiple inheritance? (EDIT: presumably virtual inheritance was a problem? EDIT2: Hmm I guess all MI would be a problem with a C-style cast presumably you had to add on the sizeof the first base to cast to the 2nd via a cast to char*)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
reinterpret_cast<> is just slightly less evil than const_cast<> IMO. It generally should only be used in dire circumstances and/or where there is a need to interact with poorly specified code and/or code from languages other than C++.

One concrete example is moving from void* to any other pointer type. If you use void* to represent "any pointer type possible", the correct way to recover a real pointer is with reinterpret_cast<>. This can be useful when a C-style API (such as the Windows API) requires a void* as an opaque parameter but you know the actual type that should be passed through; examples include any callback system with a custom parameter in the Windows API.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Well, you learn something new every day ;)

What used to happen before the new C++ casts? They're templated and were templates introduced at the same time as multiple inheritance? (EDIT: presumably virtual inheritance was a problem? EDIT2: Hmm I guess all MI would be a problem with a C-style cast presumably you had to add on the sizeof the first base to cast to the 2nd via a cast to char*)

I have to correct me a bit, but I'm still a bit uncertain about the exact details so keep that in mind.

It seems to be that is not that C-style cast cannot cast with multiple inheritance which I implied, but that the behavior of the C-style cast can silently change. The correct way to cast between base classes in multiple inheritance, as I understand it, is via static_cast and derived classes. The C-style cast will do the right thing if if the full definition of the inheritance hierarchy is present. If not, the C-style cast will go ahead and do an incorrect reinterpret_cast, while the correct static_cast will fail.

The C-style cast will therefore cast correctly when the cast is doable, but will silently work and do the wrong thing when the cast cannot be done correctly.

reinterpret_cast<> is just slightly less evil than const_cast<> IMO. It generally should only be used in dire circumstances and/or where there is a need to interact with poorly specified code and/or code from languages other than C++.

One concrete example is moving from void* to any other pointer type. If you use void* to represent "any pointer type possible", the correct way to recover a real pointer is with reinterpret_cast<>. This can be useful when a C-style API (such as the Windows API) requires a void* as an opaque parameter but you know the actual type that should be passed through; examples include any callback system with a custom parameter in the Windows API.

what about static cast? shouldn't i use it exactly for that?

Yeah, I thought static_cast would be good enough to cast from void* and back? reinterpret_cast is for when you don't want the actual value (i.e. the address held by the pointer) to change I thought...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Apparently there is some religious debate over whether to use static_cast<> or reinterpret_cast<> for that case. I suggest reading up on the controversy yourself and drawing your own conclusion; I personally prefer reinterpret_cast<> but my reasons are entirely subjective and may not be worth emulating :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement