Summary of the virtual function tables in C ++ and the functions
The virtual function table is complex in the inheritance hierarchy. The important points are summarized as follows: 1. The virtual function pointer is generally at the beginning or end of the object. 2. The virtual function table is actually an array of the function pointer type. 3. How does the compiler know the virtual function table pointer? The answer is that the last element of the virtual function table is NULL4. When multiple base classes are derived, the derived class inherits the virtual function pointer of all base classes. 5. The virtual function table is class-level, all objects of the class share the same virtual function table. 6. Objects of different classes have different virtual function tables. 7. In inheritance, when a virtual function is added to the subclass, a virtual function table is not added separately. Instead, the virtual function is placed in the inherited virtual function table of the first base class.
C language problems virtual functions
The role of a virtual function is to adjust the function to the derived class through the pointer of the base class or reference.
The code above demonstrates the role of a virtual function.
You can delete all the virtual functions in this program, run the program, and observe the two results are different. It is estimated that you can understand the role of the virtual function.
Virtual functions in c/c ++
B B B = new B (); // This sentence is incorrect and cannot contain parentheses.
Your question can be explained as follows:
Class A {virtual void fun ();}
Class B: {
Virtual void fun ();
}
Int main (){
B B = new B;
B. fun (); // call B's fun ();
A * p = & B;
P-> fun (); // call B's fun ();
Return 0;
}
Reference: www.vckbase.com/document/viewdoc /? Id = 950