Summary
This article embarks from the 5 code instance, through the class class in the ordinary inheritance, the class's virtual inheritance, the class's multiple inheritance, the multiple virtual function class's common inheritance, the virtual inheritance and the multiple inheritance, several cross-concept, elaborated the inheritance, the virtual function and the virtual inheritance the basic concept, in-depth analysis inherits from the virtual inheritance the difference from the
"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, if this is changed to direct inheritance, So sizeof (c) =12int main () { printf ("%d\n", sizeof (c)); return 0; }
Parsing: It is necessary to understand the effect of the virtual inheritance base class on the spatial size of the derived class, and to understand what spatial changes the virtual pointer brings to the subclass 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 here to pay attention to minus 4, because B and C inherit a at the same time, only need to save a copy of a, sizeof (D) =4 (A's copy) +4 (virtual table of B) +4 ( C's virtual table) =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's how to differentiate:
① There is no inheritance, the existence of virtual functions need to add a virtual pointer, if there are more than just add one, because there is only one 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;};/ /<span style= "font-family:arial, Helvetica, Sans-serif;" > similarly 16</span>class derived:public Base1, public Base2 {public: void Print2 () { } void DUMP2 () { }private: int d;};/ /16+16-4+4=32
Parse: Here is clearly pointed out that the knowledge point, if it is not a virtual inheritance of the class, even if there is virtual function will not increase storage space, if it is virtual inheritance of tears, no virtual function to add a virtual pointer space, there are virtual functions no matter how many, add 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: Here is clearly pointed out that the knowledge point, if it is not a virtual inheritance of the class, even if there is virtual function will not increase storage space, if it is virtual inheritance of tears, no virtual function to add a virtual pointer space, there are virtual functions no matter how many, add two virtual pointer space.
Summary
Important things to say three times!!!
If it is not a virtual inheritance of the class, even if there is virtual function will not increase storage space, if it is virtual inheritance of tears, no virtual function to add a virtual pointer space, there are virtual functions no matter how many, add two virtual pointer space!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + deep understanding of inheritance and direct inheritance