One way is to make the virtual function in the base class and then override it in the derived class. You do not need to define a function in the base class (although you can), but you can use pure virtual syntax in the base class virtual void foo() = 0; if you do not want to provide an implementation in the base class.
This will allow you to override it in the derived class and still call it in the base class using the base class pointer. This is known as polymorphism.
You can also just pass it to the derived type at run time if you are sure that it is actually a pointer to the derived class at that time. If it is not, it will work.
source share