I had the following code, on which I tried doing a dynamic_cast
:
struct Base {
Base() {}
~Base() {}
};
struct Derived: public Base {
Derived() {}
~Derived() {}
};
struct AnotherClass: public Base {
AnotherClass() {}
~AnotherClass() {}
};
int main() {
Derived* d = new Derived();
Base* base = d;
AnotherClass* ac = dynamic_cast<AnotherClass*> (base);
}
This led to the following error:
the operand of a runtime
dynamic_cast
must have a polymorphic class type.
Making the Base class destructor virtual
fixes this.
But why does the destructor need to be virtual here for base
to be considered to have a polymorphic class type?
Since AnotherClass
and Derived
inherit from Base
, aren't they already considered polymorphic types?