nstudent s;
naccount* pa =
&s;
nstudent* ps;
nps =
dynamic_cast<student*>(pa); //result is valid
nsavings i;
npa = &i;
nps =
dynamic_cast<student*>(pa); //result is a zero
n
nThe dynamic_cast operator relies on the dynamic type information stored in the object pointed to.
nUnlike static_cast, the dynamic_cast operator provides a safe downcast mechanism by returning a zero pointer if the object pointed to is not in the hierarchy of the type being cast to.
nIf it is, a valid pointer is returned.