You are not logged in.
Pages: 1
class A {...}
class B {...}
class C : public A, public B {...}
C * ptr = new C;
B * castOne = static_cast<B *>(ptr); // correct!
B * castTwo = reinterpret_cast<B *>(ptr); // wrong!! Points to A, not to B!
Offline
Ok... have to admit I did NOT read the links in the third message of this thread but...
A static cast in C++ is really little more than the equivalent of a ( type ) var style cast in languages such as C... and checks and validations along with warnings and errors are generated during the compilation cycle...
A reinterpret_cast is usually used to cast to ( and from ) ptrs to appropriate primitive integral type values... and on the 'stupid pet tricks' side to do things such as casting method / function ptrs from one type to another ( e.g. a function in 1 class to pretend to be a function in another class )...
So... since it seems the msg drifted back to vtables...
Perhaps what you are trying to do can be done but... Im not 100% certain what you are attempting to do... and either way - I am going to guess that it is VERY dangerous... and the reason why is a reinterpret_cast mostly just guarantees that if you do the cast in one direction, that a cast back in the other direction ( back to the original type ) will yield the same result... little to nothing more...
There are also issues with reinterpret_cast converting up and down the inheritance hierarchy for an object... its not designed to do that - and though your compiler might allow it... uh... don't do it... the thing is that ( most of the ) compilers will not do full validation... they make certain only that things such as bit width ( e.g. ptr to int ) are compatible during compile... not during runtime...!
If you need to bounce up and down inheritance levels - use the dynamic_cast operation instead... it will delve into the RTTI ( run time type information ) level when necessary in a UNIFORM manner across compilers... it will catch outright errors during compilation... and the rest will be caught during run time ( std::bad_cast err )...
I am still curious about this code you are / have been working on... is it a legacy library that is not working correctly... or ? A lot of items you have mentioned ( in other msgs ) seem to be aimed at working around deficiencies in the current object hierarchy / organization... if you have the source... perhaps abstracting out another layer or two to use more static_casts and dynamic_casts ( where required and if possible ) - would be the best answer...
Michael
"The only difference between me and a madman is that I'm not mad."
Salvador Dali (1904-1989)
Offline
Pages: 1