Let us say that I have a class A which is extended by B and C. Now, for want of polymorphism, I use base class pointer to point to the derived class object.
A *a = new B(); OR A *a= new C();
Now, let us say that I have executed few statements. now, I am interested in identifying the type of the object that the base class pointer is pointing to. How can this be done?
The direct way would be to use
dynamic_cast:however the real answer is you don’t need to when you do polymorphism right – you have to use virtual functions and implement them appropriately in derived classes so that you can blindly call them and have the right code executed.