Sizeof Calculation of class object size (continued)

Source: Internet
Author: User

In the previous article, we talked about virtual inheritance in this computation. As we can imagine, there is another important multi-inheritance in C ++ that has not yet been mentioned. Next we will talk about multi-inheritance in the previous article. Let's look at an example first:

# Include <iostream> <br/> using namespace STD; <br/> Class A <br/>{< br/> PRIVATE: <br/> int m_aa; <br/> Public: <br/> A () <br/> {<br/> cout <"A: A ()" <Endl; <br/>}< br/> ~ A () <br/>{< br/> cout <"::~ A () "<Endl; <br/>}< br/>}; <br/> Class B <br/>{< br/> PRIVATE: <br/> int m_bb; <br/> Public: <br/> B () <br/>{< br/> cout <"B: B () "<Endl; <br/>}< br/> ~ B () <br/>{< br/> cout <"B ::~ B () "<Endl; <br/>}< br/>}; <br/> Class C: Public, public B <br/>{< br/> PRIVATE: <br/> int m_cc; <br/> Public: <br/> C (): (), B () <br/>{< br/> cout <"C: C ()" <Endl; <br/>}< br/> ~ C () <br/>{< br/> cout <"C ::~ C () "<Endl; <br/>}< br/>}; <br/> int main (INT argc, char * argv []) <br/>{ <br/> cout <"sizeof A:" <sizeof (a) <Endl; <br/> cout <"sizeof B: "<sizeof (B) <Endl; <br/> cout <" sizeof C: "<sizeof (c) <Endl; <br/> return 0;

Output: sizeof A: 4

Sizeof B: 4

Sizeof C: 12

It can be seen that in Multi-inheritance (without virtual functions), The subclass size is the sum of the sizes of multiple base classes and the newly added data. If it is a virtual inheritance, it will only increase the size of an indirect pointer.

In the above example, there is no virtual function in the class. What is the result of the virtual function in the class?

See the following example:

# Include <iostream> <br/> using namespace STD; <br/> Class A <br/>{< br/> PRIVATE: <br/> int m_aa; <br/> Public: <br/> A () <br/> {<br/> cout <"A: A ()" <Endl; <br/>}< br/> virtual void foo1 () {}< br/> ~ A () <br/>{< br/> cout <"::~ A () "<Endl; <br/>}< br/>}; <br/> Class B <br/>{< br/> PRIVATE: <br/> int m_bb; <br/> Public: <br/> B () <br/>{< br/> cout <"B: B () "<Endl; <br/>}< br/> virtual void foo2 () {}< br/> ~ B () <br/>{< br/> cout <"B ::~ B () "<Endl; <br/>}< br/>}; <br/> Class C: Public, public B <br/>{< br/> PRIVATE: <br/> int m_cc; <br/> Public: <br/> C (): (), B () <br/>{< br/> cout <"C: C ()" <Endl; <br/>}< br/> ~ C () <br/>{< br/> cout <"C ::~ C () "<Endl; <br/>}< br/>}; <br/> int main (INT argc, char * argv []) <br/>{ <br/> cout <"sizeof A:" <sizeof (a) <Endl; <br/> cout <"sizeof B: "<sizeof (B) <Endl; <br/> cout <" sizeof C: "<sizeof (c) <Endl; <br/> return 0; <br/>}

Output: sizeof A: 8

Sizeof B: 8

Sizeof C: 20

Because a virtual function is added to Classes A and B, the virtual function table and virtual pointer will be created for them during compilation, so their size is increased by 4 bytes. Class C is still calculated based on the original calculation method. It has two virtual function tables and two virtual pointers (inherited ). Assume that class C defines a new virtual function, and its size does not change, because it has inherited the virtual function table from a and B, add your own virtual functions to the table instead of creating a new table.

It is almost the same here. Due to my level issues, there may be errors. You are welcome to criticize and discuss them!

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.