Polymorphism in C++ is based on the use of Virtual Function Pointer Tables (VFPTs)
-
Each class that defines virtual functions is endowed with a VFPT.
-
Table entries contain pointers to the virtual functions for that class.
-
Classes publically derived from some base have pointers to all the base's virtual functions in the VFPT by default.
-
For each virtual function the derived class overrides, by providing a definition, the VTBL points to the overriding code,
instead of the base version of that function.
-
The C++ language supports definition of a base pointer initialized with the address of an instance of any of its derived classes.
That pointer can be used to invoke any function declared in the base interface, whether virtual or non-virtual.
-
If the function called is an overridden virtual function, then the derived's definition is invoked. otherwise the base definition is invoked.
-
This assumes that the derived class has not defined a non-virtual function also provided by the base, which you should never do. Yes, never,
even though code that does this will compile, because it then runs with incorrect and possibly disasterous semantics.