The key to polymorphism is that when a virtual function is called through a base-class pointer or reference, the compile-time is not sure whether a function called a base class or a derived class is invoked, and the runtime is determined. Example:
#include <iostream>using namespace Std;class a{public:int i;virtual void func () {};virtual void Func2 () {}; //If only one is removed the virtual key is virtual void Func2 () {}; void Func2 () {}; the output remains unchanged as 8,12}; When the virtual keyword is removed, the result is 4,8class B:p ublic int j;void func () {}};int main () {cout<<sizeof (A) << "," << sizeof (B);};
Output Result:
8,12
If you remove the virtual keyword from your program:
#include <iostream>using namespace Std;class a{public:int i;void func () {};void Func2 () {};}; Class B:p ublic a{int j;void func () {}};int main () {cout<<sizeof (A) << "," <<sizeof (B);};
Output Result:
4,8
It is found that, with virtual functions, objects occupy more than 4 bytes of storage space than when there is no virtual function. In fact, any object with a virtual function and its derived class contains this extra 4 bytes, which is the key to implementing polymorphism--it is at the very front end of the object storage space, where the address of the virtual function table is stored.
Every class that has a virtual function (or a derived class with a virtual function) has a virtual function table with a pointer to the virtual function table in any object of the table (which can be thought to be done by the compiler automatically added to the constructor). Virtual function tables are generated by the compiler and loaded into memory when the program is run. The virtual function table for a class lists all the virtual function addresses of the class. For example, in the above program, the storage space of the Class A object and the virtual function table (assuming that class A has other virtual functions)
Storage space for Class B objects and virtual function tables (assuming Class B has other virtual functions)
A polymorphic function call statement is compiled to look up the virtual function address in a virtual function table and invoke a series of instructions for a virtual function, based on the address of the virtual function table that the base class pointer points to (or the base class reference refers to) the object.
Assuming the type of PA is a *, the PA->FUNC () statement executes as follows:
(1) Remove the first 4 bytes of the position referred to by the PA pointer, that is, the address of the virtual function table of the class to which the object belongs (in a 64-bit computer, the pointer takes up 8 bytes, so the 8 bytes are removed). If the PA points to an object of Class A, the address is the address of the virtual function table of Class A, and if the PA points to an object of Class B, the address is the address of the virtual function table of Class B.
(2) Find the virtual function table based on the address of the virtual function table, and find the address of the virtual function to be called. It may be considered that the virtual function table is looked up with the function name as an index, although there is a more efficient way to find it. If the PA points to an object of Class A, it will naturally look for the address of the A::func in the virtual function table of Class A, and if the PA points to an object of Class B, the address of the B::func is detected in the virtual function table of Class B. Class B does not have its own FUNC2 function, so the address of the A::FUNC2 is saved in the virtual function table of Class B, so that even if PA points to the object of Class B, "Pa->func2 ();" This statement can also find the address of A::FUNC2 in the virtual function table of Class B during execution.
(3) The virtual function is called based on the address of the virtual function found.
As can be seen from the above process, as long as it is through the base class pointer or the base class reference to call the virtual function of the statement, it must be polymorphic. It is also bound to perform the above checklist procedure, even if the virtual function is only available in the base class, not in the derived class.
New standard C + + programming
The principle of polymorphic implementation------New standard C + + programming