[C++] Polymorphism ⑦ (Polymorphism mechanism implementation principle | Virtual function table concept | Virtual function table working mechanism | vptr pointer | Virtual function table runtime mechanism | Virtual function and dynamic binding)

Article directory

  • 1. The principle of polymorphism
    • 1. Three conditions for polymorphism to be established
    • 2. The concept of virtual function table
    • 3. Working mechanism of virtual function table
    • 4. vptr pointer
    • 5. Virtual function table runtime mechanism
    • 6. Virtual functions and dynamic binding
  • 2. Code example – virtual function table
    • 1. Code example analysis – creation and use of virtual function table
    • 2. Complete code example

1. Principle of polymorphism

1. Three conditions for polymorphism

“Polymorphism” implementation needs to meet the following three conditions:

  • First of all, there must be inheritance relationship;
  • Then, the function in the parent class needs to be modified with the virtual keyword, the subclass rewrites the “virtual function” ;
  • Finally, parent class pointer or parent class reference points to the object of the subclass;

Polymorphism can be achieved by meeting the three conditions of ① inheritance, ② virtual function rewriting, and ③ parent class pointer/reference pointing to subclass object;

2. Concept of virtual function table

” polymorphism” mechanism, implemented by “virtual function table”;

” Virtual Function Table ” , The English name is ” Virtual Function Table ” , referred to as Vtable;

The C++ compiler puts the virtual function pointer into the virtual function table of the base class to call the corresponding virtual function according to the type of the actual object at runtime;

The virtual function table is automatically maintained by the C++ compiler and is transparent to programmers;

3. Working mechanism of virtual function table

“Virtual function table” is created and maintained by the C++ compiler. Virtual functions modified by the virtual keyword will be automatically stored in the “virtual function table” by the C++ compiler;< /strong>

Virtual function table creation: When you use the virtual keyword in a class to declare a virtual function, the C++ compiler will Automatically generate “virtual function table” for this class;

  • The prerequisite for generating a virtual function table is that has at least 1 virtual function;
  • If there is no virtual function, the virtual function table will not be generated;
  • If there is a virtual function in the class, then each object of the class has a vptr pointer pointing to the virtual function table; font>

The virtual function table stores virtual function pointers: ” virtual function table” is storage” class member function pointers ” data structure, is an array of function pointers, the elements in the array are function pointers, Specifically stored are pointers to virtual functions in the class;

  • If the virtual function of the parent class is overridden in the subclass, then the C++ compiler will put it in the subclass virtual function table Enter the function pointer of the virtual function of this subclass;

4. vptr pointer

If there is a virtual function in the C++ class, When the object is created, a virtual function table, referred to as vtable, will be generated; font>

When the C++ compiler compiles the code, will automatically add a vptr pointer member variable to the class, the pointer will point to the virtual function table;< /font>

5. Virtual function table runtime mechanism

“Virtual function table” is generated when the C++ compiler is compiled, is stored in the memory of the executable file during runtime; font>

When the program is running, according to the type information of the object, can dynamically call the corresponding function through the virtual function table;

The virtual function table has a one-to-one correspondence with the object. If the parent class pointer points to the object and calls the virtual function, the function will be searched in the virtual function table corresponding to the object, and the virtual function found must be the object’s virtual function;

The virtual function table mechanism can avoid type judgment at runtime, improving the efficiency and maintainability of the program;

6. Virtual functions and dynamic binding

The C++ compiler determines whether a function is virtual;

  • Static binding of non-virtual functions: If the function is not modified by the virtual keyword, the function is not a virtual function, and the function can be determined as an ordinary member function, then “static binding” is used. Whether to call this function can be determined at compile time;
  • Dynamic binding of virtual functions: If the function is modified by the virtual keyword, the function is a virtual function, C + + When the compiler compiles this class, it will automatically generate a virtual function table, and set a vptr pointer for the object, pointing to the virtual function table, When calling, you need to find the virtual function in the virtual function table pointed to by vptr. The operation of finding a virtual function is performed at runtime. This is ” dynamic linking” ;

2. Code example – virtual function table

1. Code example analysis – virtual function table creation and use

In the following code, Parent is the parent class, which defines a virtual function, and the virtual function is overridden in the Child subclass;

Using the following code, when creating a Child subclass object, it is found that a virtual function will create a virtual function table, and a vptr pointer member variable will be automatically added to the object to point to the first address of the virtual function table;

 // When creating a Child subclass object
// If a virtual function is found, a virtual function table will be created
// Use the vptr pointer in the object to point to the first address of the virtual function table
Child c;

The virtual function table stores the entry addresses of multiple virtual functions;

When assigning the address of the Child object to the Parent * pointer, the fun virtual function is called through the Parent * pointer.

The C++ compiler does not care at all whether the currently called function is from the Parent class or the Child subclass.

Instead, the corresponding virtual function is called according to the virtual function table pointed to by the vptr pointer in the object;

Both the parent class object and the subclass object have a vptr pointer member variable,

When a virtual function is called, the corresponding virtual function in the virtual function table will be searched based on the vptr pointer in the object;

2. Complete code example

Code example:

#include "iostream"
using namespace std;

// father
class Parent {<!-- -->
public:
virtual void fun(int a)
{<!-- -->
cout << "Execute parent class virtual void fun(int a) function" << endl;
}
};

// Subclass
class Child : public Parent {<!-- -->
public:
virtual void fun(int a)
{<!-- -->
cout << "Execute subclass virtual void fun(int a) function" << endl;
}
};

int main() {<!-- -->

Parent* p;
//When creating a Child subclass object
// If a virtual function is found, a virtual function table will be created
// Use the vptr pointer in the object to point to the first address of the virtual function table
Child c;

// Point the parent class pointer to the subclass object
p = &c;
// Call the fun function of the subclass object through the parent class pointer
p->fun(1);

//The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}

Execution results:

Execute subclass virtual void fun(int a) function
Press any key to continue . . .

syntaxbug.com © 2021 All Rights Reserved.