///First let's understand the order in which class objects are constructed. #include <iostream>using namespace STD;classa{ Public: A () {cout<<"A"<< Endl; }Virtual voidPRINTFA () =0;};classB Public: B () {cout<<"B"<< Endl; }};classE |Virtual PublicA Publicb{//About constructing the object order: The first step is to construct a virtual base class, such as virtual inheritance here. //The second step constructs a generic inheritance class, which is executed from left to right. //The third part constructs member variables, remember that the construction of member variables here is executed sequentially. //Fourth constructs its own constructor. Public: C () {cout<<"C"<< Endl; }voidPRINTFA () {cout<<"Hello word!!"<< Endl; }Private:};intMain () {C C;return 0;}
#include <iostream>using namespace STD;typedef void(*pfun) ();classbase{ Public:Virtual voidPrintf () {cout<<"Base::P rintf ()"<< Endl; }voidPrintfbae () {cout<<"Base::P rintfbase"<< Endl; }};classSon1: Publicbase{ Public:Virtual voidPrintf () {cout<<"Son1::P rintf ()"<< Endl; }voidPrintfSon1 () {cout<<"Son1::P rintfSon1 ()"<< Endl; }};classSon2: Publicson1{ Public:voidPrintf () {cout<<"Son2::P rintf ()"<< Endl; }Virtual voidHello () {cout<<"Hello"<< Endl; }voidPrintfSon2 () {cout<<"Son2::P rintf ()"<< Endl; }};intMain () {Son2 s2; Base &b = s2;//b.printf (); ///Here we can conclude that these three classes maintain a common virtual table, but we have no way of knowing their //location and the storage relationships of non-virtual functions within each class, so we'll verify next. //pfun fun = (pfun) * ((int *) (& (int*) &b) +1);Pfun fun = (pfun) * ((int*)*(int*)(int*) (&B) +0);//This practice is basically no one to explain, so I explained in the back with the figure. Fun (); Fun = (pfun) * ((int*)*(int*)(int*) (&B) +1); Fun ();//--------------------------------------------------------Fun = (pfun) * ((int*)*(int*)(int*) (&S2) +0); Fun (); Fun = (pfun) * ((int*)*(int*)(int*) (&B) +1); Fun ();//Printing results as above, this virtual table is shared by all member classes that inherit the base class of the owning virtual table. //Each object must be produced with two data, a pointer to the virtual table, and its own members vptr //variable. return 0;}//Summary 1: The premise is that the base class is a virtual table, single inheritance, all the member classes and base classes together maintain the same virtual table, each has//A vptr points to this virtual table, and the subclass will replace the virtual table, depending on whether the subclass has overloaded the base class//virtual function to determine that we become covered. Multiple inheritance is discussed below, in fact the essence is the same, the key problem is that multiple inheritanceThere are several virtual vptr pointers in the//subclass.
#include <iostream>using namespace STD;classa{ Public:Virtual voidPrintf () {cout<<"A::P rintf ()"<< Endl; }Virtual voidPRINTFA () {cout<<"A::P RINTFA ()"<< Endl; }};classb{ Public:Virtual voidPRINTFB () {cout<<"B::P rintf ()"<< Endl; }Virtual voidPrintf () {cout<<"B::P rintf ()"<< Endl; }};classE | PublicA Publicb{ Public:voidPrintf () {cout<<"C::P rintf ()"<< Endl; }};intMain () {typedef void(*pfun) (); c C; Pfun fun = (pfun) * ((int*)*(int*)((int*) (&C) +0) +0);//The first virtual table pointer vptr. Fun ();//Covered here, call the printf () in C;Fun = (pfun) * ((int*)*(int*)((int*) (&C) +1) +0);//Second virtual table pointer vptr. Fun ();//Here's another highlight, which is why (int *) (int *) two times, you should understand, because it may //There are multiple virtual pointers, the compiler does a good job, if you do not compile will error. cout<<sizeof(c) << Endl;//Ultimate test Result 8. return 0;}//Summary 2: Each base class in multiple inheritance maintains one of its own virtual tables, and each inheritance of a virtual base class in a subclass has a//Pointer to the base class virtual table, the test result of the above code is that there are two vptr pointers in the C object, a virtual table pointing to a base class, a//A virtual table pointing to the B-base class.
C + + Join me in a thorough understanding of virtual function tables