C + + Join me in a thorough understanding of virtual function tables

Source: Internet
Author: User

///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;};classD = Public: B () {cout<<"B"<< Endl; }};classE |Virtual  PublicA Publicb{//About constructing the object order:    The first step is to construct the virtual base class, such as the virtual inheritance here.

//The second step constructs a generic inheritance class. This runs from left to right. //The third part constructs member variables, remember that the construction of member variables here is run sequentially.

The IV constructs its own constructor.

public: C() { cout << "C" << endl; } void PrintfA() { cout << "hello word!!" << endl; }private:};int main(){ 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    Detailed location and the storage relationships of non-virtual functions within each class. So we're going to verify next.

//pfun fun = (pfun) * ((int *) (& (int*) &b) +1);Pfun fun = (pfun) * ((int*)*(int*)(int*) (&AMP;B) +0);//Such a practice is basically no one to explain, so I explained in the back with the figure. Fun (); Fun = (pfun) * ((int*)*(int*)(int*) (&AMP;B) +1); Fun ();//--------------------------------------------------------Fun = (pfun) * ((int*)*(int*)(int*) (&AMP;S2) +0); Fun (); Fun = (pfun) * ((int*)*(int*)(int*) (&AMP;B) +1); Fun ();//Printing results as above, it is possible to conclude that this virtual table is shared by all of the member classes that have virtual table base classes. //The generation of each object must have two data. A vptr pointer to a virtual table, and its own members //variable.

return 0;} //Summary 1: The premise is that the base class is a virtual table, single inheritance, all the member classes and the base class together maintain the same virtual table, each has //A vptr points to this virtual table, the subclass assumes that the virtual table will be replaced, in detail depending on whether the subclass has overloaded the base class //virtual function to determine that we become covered.

多继承以下讨论,事实上本质是一样的,关键问题是,多继承中//子类中究竟有几份虚vptr指针。

#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*) (&AMP;C) +0) +0);The first virtual table pointer vptr.

Fun (); //Covered here. The call is printf () in C; fun = (pfun) * ((int *) * (int *) (int *) (&C) + 1) + 0< /c10>); //Second virtual table pointer vptr. Fun (); //The other highlight here is why (int *) (int *) two times. You should be clear about that. Because there may //have more than one virtual pointer. The compiler is doing very well, assuming that compiling without this will cause an 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 above code tests the result that there are two vptr pointers in the C object. A virtual table that points to a base class, one //A virtual table that points to the B-base class.

C + + Join me in a thorough understanding of virtual function tables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.