The virtual function table completes the implementation of virtual functions by the C ++ compiler, that is, dynamic binding. To achieve dynamic binding (later binding), the C ++ compiler uses a table, during the re-execution period, "indirectly" calls the function actually to be bound. Such a table becomes a virtual function table vtable)
For each "class containing virtual functions", the compiler creates a virtual function table for it. Each element in the table points to the address of a virtual function.
In addition, the compiler will add a member variable to the class, which is a pointer to the virtual function (vptr), each class containing the virtual function, derived objects, there is such a vptr. When we call a virtual function with this object, we actually find the virtual function table through vptr and then find the real address of the virtual function.
The content of the virtual function table is filled with function pointers one by one based on the virtual function declaration order in the class. The derived class will inherit the virtual function table of the base class (and all other Members that can be inherited). When we rewrite the virtual function in the derived class, the virtual function table will be affected: the function address referred to by the element in the table is no longer the function address of the base class, but the function address of the derived class.
Author: "thousands of miles"