Sizeof and classes, inheritance, and virtual

Source: Internet
Author: User

[Reprinted] -- thank you for your summary.

The memory layout of objects in the virtual inheritance hierarchy is different in different compilers.

First, let's talk aboutGccCompiler.

It is easy to implement, no matter whether it is virtual inheritance or not,GccAll areShared by the virtual table pointer in the entire inheritance relationshipWhich does not share the pointer to the virtual base class.

Class {

Int;

Virtual ~ A (){}

};

Class B: virtual public {

Virtual void myfunb (){}

};

Class C: virtual public {

Virtual void myfunc (){}

};

Class D: Public B, public c {

Virtual void myfund (){}

};

AboveCodeMedium Sizeof (A) = 8, sizeof (B) = 12, sizeof (c) = 12, sizeof (d) = 16.

Explanation: A Medium Int + Virtual table pointer . B , C Because it is a virtual inheritance, the size is A + Pointer to the virtual base class, B, c Although you have added your own virtual functions, the virtual table pointers are shared with the base class, so you do not have your own virtual table pointers. . D Because B, c AllTherefore D Contains only one A Copy, So D The size is equal A + B Pointer to the virtual base class in + C pointer to the virtual base class .

If B, C if it is not virtual inheritance, but normal inheritance, A, B, c all sizes are 8 ( NO Pointer to the virtual base class ) , and d because it is not a virtual inheritance, therefore, it contains two A copy, size: 16. note that d is the same as that of virtual inheritance, but the memory layout is different.

Then, let's take a look.VCCompiler

VCProcessing ratio of virtual table pointersGccComplex, it depends onWhether it is a virtual inheritance to determine whether to share the virtual table pointer in the inheritance relationshipAnd the pointer to the virtual base class andGccIn the same way, it is not shared, and of course it cannot be shared.

The Code is the same as above.

The running result will beSizeof (A) = 8, sizeof (B) = 16, sizeof (c) = 16, sizeof (d) = 24.

Explanation: A Still Int + Virtual table pointer. B , C MediumBecause it is a virtual inheritance, the virtual table pointer is not shared., Because B, c Added your own virtual functions, so B, c Maintain a virtual table pointer, pointing to its own virtual function. (Note: The compiler will add a virtual table pointer to the subclass only when the subclass has a new virtual function)Therefore B, c The size is A + Own virtual table pointer + Pointer to the virtual base class . D Because B, c All are virtual inheritance, so D Contains only one A At the same time D Yesfrom B, c Ordinary inherited, rather than virtual inherited, so there is no virtual table pointer of its own. Therefore D The size is equal A + B Virtual table pointer + C Virtual table pointer + B Pointer to the virtual base class in + C Pointer to the virtual base class in.

Similarly, if virtual inheritance is removed, the result is consistent with GCC the results are the same. A, B, c all 8 , d is 16 because VC for non-virtual inheritance, the parent class and subclass share the virtual table pointer .

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.