so I have this code:
Base* objbase = new Derived();
//perform a downcast at runtime with dynamic_cast
Derived* objDer = dynamic_cast<Derived*>(objBase);
if(objDer)//check for success of the cast
objDer->CallDerivedFunction();
This is a snippet of code for a cast operators section in my book.
Now why do I have this, I don't understand what's the point of having to dynamically cast a pointer to a base object pointing to a Derived object; For me, that's something to do with polymorphism giving us the ability to do objBase->DeriveClassFunction()
, but I don't really know.
In the first place why does it do this: Base* objbase = new Derived();
, and then why does it cast a base object pointer to a Derived again, I can't quite figure out why.
Thanks in advance.