Here is my test example:
struct base {
virtual ~base(){}
int x;
};
struct derived: public virtual base {
base * clone() {
return new derived;
}
derived(): s("a") {}
std::string s;
};
int main () {
derived d;
base * b = d.clone();
derived * t = reinterpret_cast<derived*>(b);
std::cout << t->s << std::endl;
return 0;
}
It crashes at the line where I print s. Since "b" is a pointer to the derived class, reinterpret_cast should just work. I wonder why it crashes. At the same time, if I replace reinterpret_cast with dynamic_cast, then it works.
Even if b
is here dynamically of type derived
, you have to use dynamic_cast
. This is what dynamic_cast
is for, to dynamically convert a pointer of a base class into a derived class at runtime.
reinterpret_cast
takes the raw pointer and considers it as being of the derived type. However, because of the virtual
inheritance, a slight adjustment must be done to the pointer to point to the correct method dispatch table, and that's precisely what dynamic_cast
will do.
Don't reinterpret_cast
it, it will cause trouble with multiple or virtual inheritance, like in your case. Wont a simply static_cast
do the job here?
To know why, search for implementations of virtual inheritance. A common one is to store a pointer to the base class within the object, so the virtual base does not share the same address than its derived classes. There is a similar case when multiple inheritance is used.
In short, reinterpret_cast
can't do much more than casting pointers to ints and back (if there is enough size in the int to contain a pointer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With