Consider a class Base
with virtual functions and a class Derived
from Base implementing the virtual functions, but with few additional private members.
Can we safely downcast the Base*
pointer to Derived*
pointer?
Base* base = new Derived();
Derived* derived = dynamic_cast<Derived*>(base); // Is this valid?
What if derived class contains an additional private member int _derivedNum apart from implementation of virtual functions in the base class? Can I still use derived->_derivedNum to access the private member of the derived class after downcasting the base class pointer?
dynamic_cast< Derived * >
. – lapk