Reference: http://www.weixueyuan.net/view/6372.html
Summarize:
In C + + through the virtual member function table vtable to achieve polymorphism, virtual function table is stored in the class virtual function of the entry address.
Using polymorphism can reduce the efficiency of the program, use polymorphic programs to use more storage space, store virtual function tables and other content, and in the call function to go to the virtual function table to query function entry address, which increases the program run time.
In the design of the program, the programmer can choose the use of polymorphism, for the required functions to use polymorphism, for other functions do not use polymorphism.
Typically, if a class needs to be a base class and expects to modify the functionality of a member function in a derived class and accesses the function as a pointer or reference when using the class object, the function is declared as a virtual function.
Through the introduction of the first two sections, we must have a certain understanding of polymorphism. This section explains how polymorphism is implemented, and how to implement polymorphism, even if not known to the program designer, is of great significance for deepening the understanding of polymorphism, so a little elaboration of the polymorphic implementation mechanism in this section.
in C + + through the virtual member function table vtable to achieve polymorphism, virtual function table is stored in the class virtual function of the entry address. in ordinary classes there is no virtual function table, only in a class with a virtual function (whether the virtual function is added or inherited from the virtual function) will have a virtual function table, usually the first address of the virtual member function table will be stored in the front of the object (in 32-bit operating system, the storage address is 4 bytes, So this first address will occupy the first four bytes of the object's space.
Example 1: #include<iostream>using namespacestd;class Base{ Public: Virtual voidv1 () {}Virtual voidv2 () {}};classDerived Public Base{ Public: Virtual voidv1 () {}Virtual voidv2 () {}};intMain () {Baseb; Derived D; Base*p; P= &b; P-v1 (); P-v2 (); P= &D; P-v1 (); P-v2 (); return 0;}
We define the two classes as shown in precedent 1, with two virtual functions V1 and V2 in each of the two classes, and we find their function entry addresses in the following table:
Virtual member function |
Function Entry Address |
Virtual member function |
Function Entry Address |
Base::v1 |
00d15834 |
Derived::v1 |
00d15844 |
Base::v2 |
00d15838 |
Derived::v2 |
00d15848 |
The virtual function table stores the entry address of the virtual function. Let's look at the main function, first defining the base class object B in the main function, because there are virtual functions in class B, so there is a virtual function table, and the first address of the virtual function table is stored at the top of the storage space where the object is located, as the case may be. Of course, after declaring derived object D, the same is true in the object storage space that contains the virtual member function table address.
After defining a pointer p for a base class type, when we call the virtual function V1 or v2 through the base class pointer P, the system first looks for the virtual function table address in the first four bytes of the object that P points to, then finds the virtual function table in memory, then finds the entry address of the corresponding function in the table, and then accesses the function directly. When the P pointer points to a base class object, the virtual function table of the base class is accessed and the virtual function in the base class is called. When the P pointer points to a derived class object, it accesses the virtual function table of the derived class, which is stored in the virtual function table of the derived class as the entry address of the virtual function in the derivation class, so the virtual function in the derived class is called.
using polymorphism can reduce the efficiency of the program, use polymorphic programs to use more storage space, store virtual function tables and other content, and in the call function to go to the virtual function table to query function entry address, which increases the program run time. In the design of the program, the programmer can choose the use of polymorphism, for the required functions to use polymorphism, for other functions do not use polymorphism. Typically, if a class needs to be a base class and expects to modify the functionality of a member function in a derived class and accesses the function as a pointer or reference when using the class object, the function is declared as a virtual function .
4.3 C + + virtual member function table vtable