The member functions in a class are divided into static member functions and non-static member functions, and non-static member functions are divided into ordinary and virtual functions.
Q: Why virtual functions are used
A: With virtual functions, we can get good scalability . In a well-designed object-oriented program, most functions communicate with the interface of the base class. Because the base class interface is used, the program that calls the base class interface can adapt to the new class without changing it. If a user wants to add a new feature, he can inherit from the base class and add the appropriate new functionality.
Q: Brief Introduction to C + + virtual function and underlying implementation principle
A: The main point is to answer the virtual function table and virtual function table pointer function.
Virtual functions in C + + are implemented using virtual function tables and virtual function table pointers. The virtual function table is the Address table of a virtual function of a class, used to index the class itself and the address of the virtual function of the parent class, and if the subclass overrides the virtual function of the parent class, then the corresponding virtual function in the virtual function table is replaced with the address of the function of the subclass (the subclass may not be virtual function, but must have the same name); The virtual function table pointer exists in each object (usually at the start address of an object for efficiency), pointing to the address of the virtual function table of the class in which the object resides, and in multiple-inheritance environments, there are multiple virtual function table pointers. Points to the virtual function table corresponding to the different base classes, respectively.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------
A virtual function table is one for each class that has a virtual function. A virtual function table Pointer is a corresponding one for each object .
Ref
http://blog.csdn.net/haoel/article/details/1948051
http://songlee24.github.io/2014/09/02/cpp-virtual-table/
The principle of C + + virtual function