考点一:sizeof 计算虚继承类对象的空间大小
#include<iostream>
using namespace std;
class A
{};
class B
{};
class C : public A,public B
{};
class D:virtual public A
{};
class E:virtual public A,virtual public B
{};
class F
{
public:
int a;
static int b;
};
int F::b = 0;
int main()
{
cout << sizeof(A) << endl; // A 是空类 编译器会安插一个 char 给空类,用来标记它的对象,其大小为一
cout << sizeof(B) << endl; // B 是空类 编译器会安插一个 char 给空类,用来标记它的对象,其大小为一
cout << sizeof(C) << endl; // C 是空类 编译器会安插一个 char 给空类,用来标记它的对象,其大小为一
cout << sizeof(D) << endl; // D 虚继承A,编译器安插一个指向父类的指针,不会安插 char 了,其大小为4
cout << sizeof(F) << endl; // F 中有静态变量 ,静态变量成员不存在类中,并且被每个实例所共享 ,其大小为4
cout << sizeof(E) << endl; // E 虚继承A,也虚继承B,所以有两个指向父类的指针,其大小为 8
return 0;
}