I have a base class and derivative class, e.g :
class Base {
public:
Base();
virtual doSomthing();
};
class Derivative : class Base {
public:
Derivative();
virtual doSomthing();
};
I know that if I want to change at runtime from the father to son I will do
Derivative& newDer = dynamic_cast<Derivative&>(baseInstance)
my question is how I can do the opposite operation - change from son to the father?
There's no specific cast operation needed. Any
Derivative&automatically can be passed for aBase&if they are really have that relation like inSupposed you tried to do that from a global
publicscope.The inheritance relation needs to be accessible in the scope used though. Your example has
privateinheritance, and it won't work at thepublicscope.As mentioned by @StoryTeller and @Peter it will work at the
Derivativeinner class scope with any class member function orfriended function/class.