Daily bit(e) of C++ | virtual member functions
Daily bit(e) of C++ #462, Calling member functions based on the actual type of the object using the virtual and override keywords.
Virtual member functions modify the behaviour of calling member functions.
During object construction, the final overrider of each virtual member function is determined and stored in a table (vtable).
If the name lookup finds a virtual member function, it will insert an indirection through the vtable instead of calling it directly.
This ensures that the member function called will depend on the actual type of the object, not on the static base type listed in the code. This property is maintained even if the caller doesn’t have any information about the actual type of the object.