... a=b;
You reassign to a , and so a and b will now point to a DerivedClass object. The BaseClass object will be destroyed, because at that moment its reference count will be zero (by virtue of a , which will be reassigned to point to another object).
Since a now points to a DerivedClass object, calls to virtual functions (defined in BaseClass and override in DerivedClass ) through a will call the corresponding member functions in DerivedClass .
When both a and b disappear from scope, the DerivedClass object will be destroyed.
If you need to access functions related to a derived class through a (for example, non-virtual functions in DerivedClass ), you can use:
boost::dynamic_pointer_cast<DerivedClass>(a)->SomeFunctionOnlyInDerivedClass();
Of course, this is just a brief example showing usage. In production code, you will almost certainly experience a successful click on DerivedClass before dereferencing a pointer.
Michael Goldshteyn
source share