C + + memory layout (next) __c++

Source: Internet
Author: User

The previous article has analyzed the basic structure, C + + simple objects, plus the general inheritance of the various situations, this article will mainly in the virtual inheritance, the C + + object memory is how to allocate. Virtual Inheritance

Virtual inheritance solves the problem that classes inherited from different ways have common base classes, making common base classes only one copy. Solve the problem of two semantics, but also save memory.

The generic class diagram for virtual inheritance looks like this:

sizeof (Derived) = 40 bytes. After analysis, can get the derived class in memory structure diagram:

Summary: the storage sequence of each part in memory for the late father class, after the subclass, the last common base class, that is, first store Base1, Base2, then derived, and finally the base class for virtual inheritance, the subclass will add a vbptr pointer, it points to the value is either 0, Either-4, which represents the offset of the common base class relative to the subclass, or the base class's offset from the derived class. When there is a virtual function in the base class, is-4; otherwise, 0. In the VS compiler, subclasses and common base classes are separated by a null pointer. This field may not be in the other compilers

For each class, the virtual function table can be parsed sequentially: first, the base class, and the FF () function of the base class is first overwritten by the BASE1, then overwritten by the derived class, and the GG () function of the base class is not overwritten. Therefore, the virtual functions pointed to in the virtual function table of the Base class are: Derived::ff (), Base::gg (), respectively. Then there is the Base1 class, where the BASE1 class originally defined two virtual functions, but the FF () function overrides the function in the parent base, so it is not in the BASE1 virtual function table, only BF1 (). At the same time subclass Derived new definition of a HH () virtual function, to insert into the memory of the first virtual function table, so it will be inserted into the Base1 class of the virtual function table, so the Base1 class will have Base1::bf1 (), DERIVED::HH () two functions Finally, the Base2 class, only BASE2::BF2 () a virtual function.

The simple validation code looks like this:

typedef void (*fun) (void);
     Class base{Public:base (): BA (333) {} int ba;
    virtual void ff () {cout<< "base::ff ()" <<endl;
    virtual void Gg () {cout<< "Base::gg ()" <<endl;

}
};
    Class Base1:virtual public base{public:base1 (): BA (one) {} int ba;
    virtual void ff () {cout<< "base::ff ()" <<endl;
    virtual void Bf1 () {cout<< "base::bf1 ()" <<endl;

}
};
     Class Base2:virtual public base{public:base2 (): Ba (a) {} int ba;
    virtual void Bf2 () {cout<< "base2::bf2 ()" <<endl;

}
}; Class Derived:public Base1,public base2{public:derived ():d A () {Base1 ();
    Base2 ();}
     int da;
    void ff () {cout<< "derived::ff ()" <<endl;
    virtual void hh () {cout<< "derived::hh ()" <<endl;

}
};
    int main (void) {Derived *pt=new Derived ();
    printf ("sizeof (Derived) =%d\n", sizeof (Derived)); int Len=sizeOf (Derived)/4;
    int** Pvtab = (int**) pt;   

    Fun Pfun;
   cout<< "pt[0]:" <<endl; for (int i=0; (Fun) Pvtab[0][i]!=null;
                i++) {Pfun = (Fun) pvtab[0][i];
    Pfun ();
    } cout<< "pt[1]:" <<endl;
    cout << "[<<0<<]";

    cout<< (int) *pvtab[1]<<endl;

    cout<< "pt[2]:" << (int) pvtab[2]<<endl;
    cout<< "pt[3]:" <<endl; for (int i=0; (Fun) Pvtab[3][i]!=null;
                i++) {Pfun = (Fun) pvtab[3][i];
    Pfun ();
    } cout<< "pt[4]:" <<endl;
    cout << "[<<0<<]";

    cout<< (int) *pvtab[4]<<endl;
    cout<< "pt[5]:" << (int) pvtab[5]<<endl;
    cout<< "pt[6]:" << (int) pvtab[6]<<endl;

    cout<< "pt[7]:" << (int) pvtab[7]<<endl;
    cout<< "pt[8]:" <<endl;
    Pfun = (Fun) pvtab[8][0]; for (int i=0; (Pfun = (Fun) pvtab[8][i])!=nulL;++i) {pfun ();

    } cout<< "pt[9]:" << (int) pvtab[9]<<endl;
    System ("pause");
return 0;
 }

The

Gets the output as:

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.