Single inheritance, multi-inheritance, virtual inheritance, sizeof size

Source: Internet
Author: User

1. Question

# Include <iostream>
Using namespace STD;

Class S {};

Class A: s {
Virtual void fun (){
;
}
};
Class B: {
Virtual void fun (){
;
}
};
Class C: B {
Virtual void fun (){
;
}
};
Class M {
Virtual void fun (){}
};
Class N {
Virtual void fun (){}
};
Class P: M, N {
Virtual void fun (){}
};
//------------------------------
Class t_s {};
Class T_A: Virtual t_s {
Virtual void fun (){
;
}
};
Class T_ B: Virtual T_A {
Virtual void fun (){
;
}
};
Class T_c: Virtual T_ B {
Virtual void fun (){
;
}
};
Class T_m {
Virtual void fun (){}
};
Class t_n {
Virtual void fun (){}
};
Class t_p: Virtual T_m, t_n {
Virtual void fun (){}
};
Int main (){
//----------------------------
// Single inheritance
Cout <sizeof (s) <Endl; // 1, empty class
Cout <sizeof (a) <Endl; // 4, a virtual function pointer
Cout <sizeof (B) <Endl; // 4, a virtual function pointer
Cout <sizeof (c) <Endl; // 4, a virtual function pointer
//------------------------------
// Multi-Inheritance
Cout <sizeof (m) <Endl; // 4, a virtual function pointer
Cout <sizeof (n) <Endl; // 4, a virtual function pointer
Cout <sizeof (p) <Endl; // 8, two virtual function pointers, multiple inheritance
//------------------------------
// Single inheritance
Cout <sizeof (t_s) <Endl; // 1, empty class
Cout <sizeof (T_A) <Endl; // 4, a virtual function pointer
Cout <sizeof (T_ B) <Endl; // 4, a virtual function pointer
Cout <sizeof (T_c) <Endl; // 4, a virtual function pointer
//------------------------------
// Multi-Inheritance
Cout <sizeof (T_m) <Endl; // 4, a virtual function pointer
Cout <sizeof (t_n) <Endl; // 4, a virtual function pointer
Cout <sizeof (t_p) <Endl; // 8, two virtual function pointers, multiple inheritance
//------------------------------
System ("pause ");
Return 0;
}

2. Analysis

S is an empty class, and the default size is 1.
A inherits from S, S is null, and a itself has a virtual function. Therefore, a must have a virtual pointer, so the size of a is 4.
B inherits from A. The size of a is 4 and there is a virtual pointer. B follows the virtual pointer of A, so the size of B is 4.
C inherits from B, and B has a virtual pointer (actually follows the virtual pointer of A). Therefore, C follows the virtual pointer of B, so C is 4.
In fact, a class corresponds to a virtual table, and the above virtual pointers are used for classes. The above class A, B, and C respectively have a virtual pointer, of course, B and C are inherited.
Virtual inheritance, that is, ts, Ta, TB, and TC, does not change the virtual pointer, nor is it as mentioned in the Bolg article, virtual inheritance is equivalent to adding a virtual table pointer and a parent class.
It is worth noting that if Class A has a virtual pointer, its sub-classes will inherit the above virtual pointer no matter whether there is a virtual function, in addition, even if the subclass has a virtual function, it will not add its own virtual pointer. Each class has a virtual pointer at most and there is a virtual table at most.
M has a virtual pointer, N has a virtual pointer, P inherits M and N, there are two virtual pointers, so the size of P is 8.

PS: for 32 machines, the pointer length is 4 bytes.

3. Adding a question is slightly different from the previous one.

#include <iostream>
using namespace std;

class S {};
class A:public S {
public:
virtual void func() { cout << "A" << endl; }
};
class B:public A {
public:
virtual void func() { cout << "B" << endl; }
};
class C:public A {
public:
void func() { cout << "C" << endl; }
};
class D:public C {
};

int main() {
A* pa = new A;
A* pb = new B;
A* pc = new C;
pa->func(); // A
pb->func(); // B
pc->func(); // C
cout << sizeof(S) << endl; // 1
cout << sizeof(A) << endl; // 4
cout << sizeof(B) << endl; // 4
cout << sizeof(C) << endl; // 4
cout << sizeof(D) << endl; // 4
system("PAUSE");
return 0;
}

The main difference is that C-class func functions are not virtual functions, so PC-> func () output is still C, because the implementation of dynamic compilation only requires the base class as virtual functions, because the base class has virtual functions, the base class must have virtual pointers, and The subclass must have virtual pointers. Therefore, dynamic compilation does not need to define the subclass as a virtual function.

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.