Jump to content
  • Advertisement
Sign in to follow this  
noatom

reinterpret_cast examples?

This topic is 1877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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*)

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!