reinterpret_cast examples?

Started by
39 comments, last by Brother Bob 10 years, 11 months ago
Other than converting back to the original type, the standard also says that if you convert a null pointer of one type to another pointer type the result will be a null pointer of the new type, so you can check a reinterpret_cast'ed pointer for null. A reinterpret_cast on things like integers and pointers to their own type will also yield the value of the original operand. Pretty much everything is either undefined, unspecified or implementation defined.
Advertisement

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.

As far as I know, C-style cast in C++ is defined to try to do a static cast, and if it can't, then do a reinterpret cast. So it should handle multiple inherritance fine. However, if you have a bug and you're doing something where it is not possible to do the cast as a static cast, then the C-style cast will do a reinterpret cast, hiding the bug.

There's also constructor-style cast, which works just like C-style cast, except when it calls a constructor.

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.

As far as I know, C-style cast in C++ is defined to try to do a static cast, and if it can't, then do a reinterpret cast. So it should handle multiple inherritance fine. However, if you have a bug and you're doing something where it is not possible to do the cast as a static cast, then the C-style cast will do a reinterpret cast, hiding the bug.

I have already discussed the ambiguous nature of the type of cast the C-style cast performs and why it doesn't work with multiple inheritance in some cases. It is because it silently falls back on the incorrect reinterpret_cast that it is dangerous to use.




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.


As far as I know, C-style cast in C++ is defined to try to do a static cast, and if it can't, then do a reinterpret cast. So it should handle multiple inherritance fine. However, if you have a bug and you're doing something where it is not possible to do the cast as a static cast, then the C-style cast will do a reinterpret cast, hiding the bug.


I have already discussed the ambiguous nature of the type of cast the C-style cast performs and why it doesn't work with multiple inheritance in some cases. It is because it silently falls back on the incorrect reinterpret_cast that it is dangerous to use.


You discussed why reinterpret_cast does not work with multiple inheritance. C-style cast isn't reinterpret_cast though. It tries to do a static cast first, so correctly written multiple inherritance will work with it.

Yes, C-style casts are bad, but not because of multiple inherritance.

I also said, and so did you, that C-style cast will do a reinterpret_cast if it cannot do the static_cast. When reinterpret_cast is the wrong thing to do in those cases, then so is the C-style that incorrectly proceeds with the cast. It changes the behavior of the cast, and the casting behavior between the two is different in multiple inheritance.

just my humble opinion: everyone should use the c++ explicit casts.It doesn't matter if you're doing a static cast,or a reinterpret cast.If you ever have problems,it will be a lot easier to locate the casts and recognize fast what kind of cast you are doing.

@Brother Bob,you made a wonderful example showing me why I should use static_cast when the types are releated.Kudos!

That is the correct answer. C-style casts being difficult to search (find in files, grep) for, and a cause of bugs, is my main gripe with them.

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

I'm surprised nobody has linked to this yet. It's been my most useful guide in understanding C++'s different casting abilities.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Well,Cornstalks,that's an amazing resource,thanks for sharing.

I also said, and so did you, that C-style cast will do a reinterpret_cast if it cannot do the static_cast. When reinterpret_cast is the wrong thing to do in those cases, then so is the C-style that incorrectly proceeds with the cast. It changes the behavior of the cast, and the casting behavior between the two is different in multiple inheritance.

The behavior of a c-style cast is always the same as a static cast, when a static cast is possible, which is is when upcasting multiple ingerritance. So the C cast will correctly upcast.

When static_cast doesn't work, reinterpret cast is probably not wrong; it's the only option. However, it might hide a bug where you expect static cast to be possible, but it isn't. The bug is likely elsewhere, but using static_cast may help catch it, turning a runtime error into a compile time error. But again, this has little to do with multiple inherritance.

This topic is closed to new replies.

Advertisement