reinterpret_cast examples?
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.
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.
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.
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.
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, a C-style cast trumps everything (can do all types of casts).
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.
I'm surprised nobody has linked to this yet. It's been my most useful guide in understanding C++'s different casting abilities.
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.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.
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.