C + + deep understanding of virtual inheritance, multiple inheritance, and direct inheritance

Source: Internet
Author: User

Summary

This article starts with a 5-segment code example. Through the ordinary inheritance of classes, the virtual inheritance of classes, the multiple inheritance of classes, the common inheritance of multiple virtual function classes, virtual inheritance and multiple inheritance, several cross concepts, the basic concepts of inheritance, virtual function and virtual inheritance are explained concretely. In-depth analysis of the difference in inheritance from virtual inheritance to contact.

"Exp.001-Virtual Inheritance"

#include <stdio.h>class A {public:    int a;};/ /sizeof (a) =4class b:virtual public A {public:int b;};/ /sizeof (b) =4 (a copy) +4 (virtual table pointer) +4 (self variable) =12class c:virtual public B {     };//sizeof (C) = (b copy) +4 (virtual table pointer) = 16, assuming this is a direct inheritance instead, So sizeof (c) =12int main () {    printf ("%d\n", sizeof (c));        return 0; }

Parsing: It is necessary to understand the effect of virtual inheriting base classes on the spatial size of derived classes, and to understand what spatial changes the virtual pointers bring to subclasses in virtual inheritance.

"Exp.002-Multiple Inheritance"

#include <stdio.h>class A {public:    int a;};/ /sizeof (a) = 4class b:virtual public A {};//sizeof (B) =4+4=8class c:virtual public A {        };//sizeof (C) =4+4=8class D:public B, Public c{       };//sizeof (d) =8+8-4=12 It is important to subtract 4, since B and C inherit a at the same time, just need to save a copy of a, sizeof (D) =4 (copy of a) +4 (virtual table of B) +4 (virtual table of C) =12int main () {    printf ("%d\n", sizeof (d));    return 0; }

Parsing: It is necessary to focus on the data space size of class D and to understand the effects of multiple virtual inheritance on derived class virtual pointers and derived class spaces.

"Exp.003-Common Inheritance (contains: empty class, virtual function)"

Class A   {   };  Class B   {    char ch;       virtual void func0 ()  {  }}; class C  {    char ch1;    char CH2;    virtual void func ()  {  }      virtual void func1 ()  {  }};class d:public A, public c{       int d;
   
    virtual void func ()  {  }     virtual void func1 ()  {  }};   Class E:public B, public c{       int E;       virtual void func0 ()  {  }     virtual void func1 ()  {  }};int main (void) {    cout<< "a=" <<sizeof (A) The  size of space occupied by <<endl;//result=1 empty class is 1    cout<< "b=" <<sizeof (B) <<endl;/ /result=8  1+4 Alignment   8     cout<< "c=" <<sizeof (C) <<endl;//result=8  1+1+4 Alignment 8    cout<< "d=" <<sizeof (D) <<endl;//result=12 C's copy +d itself =8+4=12    cout<< "e=" <<sizeof (E Copy of <<endl;//result=20 b +c +e itself =8+8+4=20    return 0;}
   

Here we need to distinguish:

① no inheritance, the existence of virtual functions need to add virtual pointers, assuming that there are more than just one, because there is only a virtual pointer;

② for ordinary inheritance, Class D and its own virtual function in Class E, the size is 0, because it has no virtual table.

③ for virtual inheritance. When there is one or more virtual functions in a derived class, it itself has a virtual table, pointing to its own virtual table, so add 4.

"Exp.004-virtual Inheritance (multiple inheritance and virtual functions)"

Class commonbase{    int co;};/ /size = 4class base1:virtual public commonbase          {public:    virtual void print1 () {  }    virtual void Print2 () {  }private:    int b1;};/ /4 Copy +4 virtual pointer +4 self +4 (virtual inheritance + virtual function constituent pointer one more) =16class base2:virtual public commonbase        {public:    virtual void Dump1 () {  }    virtual void dump2 () {  }private:    int b2;};/ /similarly 16class derived:public Base1, public Base2     {public:    void Print2 () {  }    void Dump2 () {  } Private:    int d;};/ /16+16-4+4=32

Parse: Assume that the class is not a virtual inheritance. Even if a virtual function does not add storage space, it is assumed that it is a virtual inheriting class. There is no virtual function to join a virtual pointer space, no matter how many virtual function, the addition of two virtual pointer space.

"Exp.005-virtual inheritance and virtual function"

Class A{public:    virtual void aa () {  }    virtual void Aa2 () {  }private:    char ch[3];};//1+4 = padded = 8clas s b:virtual public a{public:    virtual void bb () {  }    virtual void Bb2 () {  }};//8 (Dungeon) +4 (Virtual inheritance) +4 (virtual pointer) = 16 int main (void) {    cout<< "a ' s size is" <<sizeof (a) <<endl;//        4+4=8    cout<< "B ' s A copy of size is "<<sizeof (B) <<endl;//        a +4+4=16    return 0;}

Parse: Assume that the class is not a virtual inheritance. Even if there is a virtual function, it does not add storage space. The assumption is a virtual inheriting class. There is no virtual function to join a virtual pointer space, no matter how many virtual function, the addition of two virtual pointer space.

Summary

Important thing to say three times!!

Assume that you are not a virtual inheriting class, even if there are virtual functions that do not add storage space. The assumption is a virtual inheriting class. There is no virtual function to add a virtual pointer space, there are virtual functions regardless of how many. Add two virtual pointer space ...

C + + deep understanding of virtual inheritance, multiple inheritance, and direct inheritance

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.