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!