C++ polymorphism principle research

Theoretical knowledge:
When a virtual function is declared in a class, the compiler will generate a virtual function table in the class. The
virtual function table is a data structure that stores pointers to class member functions. The
virtual function table is automatically generated and maintained by the compiler
virtual member functions will be put into the virtual function table by the compiler When
there are virtual functions, each object has a pointer to the virtual function table (the C++ compiler lays out the vptr pointer for the parent class object and the subclass object in advance ; When performing the howToPrint(Parent *base) function, the C++ compiler does not need to distinguish the molecular class object or the parent class object, only need to find the vptr pointer in the base pointer.)
VPTR is generally regarded as the first class object Member
1 The realization principle of polymorphism The realization principle of polymorphism in
C++
When a virtual function is declared in a class, the compiler will generate a virtual function table in the class. The
virtual function table is a data structure that stores pointers to class member functions. The
virtual function table is The
virtual member functions automatically generated and maintained by the compiler will be put into the virtual function table by the compiler. When
there are virtual functions, each object has a pointer to the virtual function table (vptr pointer).
Insert picture description here
Insert picture description here
Insert picture description here
Note 1:
Through the virtual function table The pointer VPTR calls the rewritten function while the program is running, so the addressing operation is needed to determine the function that should be called. The ordinary member function is the function to be called at compile time. In terms of efficiency, the efficiency of virtual functions is much lower.
Note 2:
For efficiency reasons, it is not necessary to declare all member functions as virtual functions.
Note 3: C++ compiler executes the HowToPrint function without distinguishing whether it is a subclass object or a parent class object.
Insert picture description here
2 How to prove the existence of the vptr pointer
#include
using namespace std;

class A
{
public:
void printf()
{
cout<<“aaa”<<endl;
}
protected:
private:
int a;
};

class B
{
public:
virtual void printf()
{
cout<<“aaa”<<endl;
}
protected:
private:
int a;
};

void main()
{ //Add the virtual keyword C++ compiler will add a pointer to the virtual function table. . . printf("sizeof(a):%d, sizeof(b):%d \n", sizeof(A), sizeof(B)); cout<<"hello..."<<endl; system("pause") ; return; } 3 Can virtual functions be called in the constructor to achieve polymorphism? 1) When is the VPTR pointer in the object initialized?







When the object is created, the VPTR pointer is initialized by the compiler.
Only when the construction of the object is completed, the VPTR point is finally determined
. The VPTR of the parent object points to the parent virtual function table
. The VPTR of the child object points to the child virtual function table.

2) Analysis process
Drawing analysis
Insert picture description here

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/109114344