We know that dynamic_cast<void*>
will cast a pointer to the pointer to the most derived object; but what if the underlying object is not the most derived? For example:
class BaseClass { public: virtual void dummy() { std::cout << "Base\n"; } };
class DerivedClass : public BaseClass {
int a{};
public:
void dummy() { std::cout << "Derived\n"; }
};
class MostDerivedClass : public DerivedClass {
int b{};
public:
void dummy() { std::cout << "Most\n"; }
};
BaseClass* basePtr_d = new DerivedClass, *basePtr_md = new MostDerivedClass;
DerivedClass* derivedPtr =
dynamic_cast<DerivedClass*>(basePtr_d); // right
MostDerivedClass* mostDerivedPtr =
dynamic_cast<MostDerivedClass*>(basePtr_md); // right
MostDerivedClass* mostDerivedPtr2 =
static_cast<MostDerivedClass*>(dynamic_cast<void*>(basePtr_md)); // right
DerivedClass* derivedPtr2 =
static_cast<DerivedClass*>(dynamic_cast<void*>(basePtr_d)); // What happens??
What happens for the last case?