Maybe my question is not perfectly formed, but my code will make everything clear.
#include <iostream> using namespace std; struct A{int n;}; struct B{int n;}; struct C : A, B{}; int main() { C c; C* pc = &c; std::cout<<"TEST1"<<std::endl; cout << static_cast<B*>(pc) << "\n"; cout << reinterpret_cast<B*>(pc)<<"\n\n"; std::cout<<"TEST2"<<std::endl; cout << static_cast<A*>(pc) << "\n"; cout << reinterpret_cast<A*>(pc)<<"\n"; } And the output is:
TEST1 0042F830 0042F82C TEST2 0042F82C 0042F82C I know that using reinterpret_cast is ill formed design. I am not thinking about the design but the behavior is what bother me. Can anyone explain why casting different ways gives different results the first time but the same result the second time??
reinterpret_castis almost always indicative of bad design. Avoid it and rethink the approach.static_cast<B*>(pc)- "give me the location of theBpart of*pc;reinterpret_cast<B*>(pc)- "treatpcas the location of aB".