一.问题引入
class Grand
{
public:
int m_grand;
};
class A1:public Grand
{
public:
};
class A2 :public Grand
{
public:
};
class C1 :public A1, public A2
{
public:
};
int main()
{
cout << sizeof(Grand) << endl;// 4
cout << sizeof(A1) << endl; // 4
cout << sizeof(A2) << endl;// 4
cout << sizeof(C1) << endl;// 8
C1 c;
// c.m_grand = 0;// 这个是错误的;访问不明确
c.A1::m_grand = 0;
c.A2::m_grand = 0;
return 0;
}
问题:当子类C1的两个父类A1 A2有共同的基类Grand的时候,就会存在存在子类C1继承了两次Grand里面的内容;
二.虚基类的引入
class Grand
{
public:
int m_grand;
};
class A1:virtual public Grand
{
public:
};
class A2 :virtual public Grand
{
public:
};
class C1 :public A1, public A2
{
public:
};
int main()
{
cout << sizeof(Grand) << endl;// 4
cout << sizeof(A1) << endl; // 8
cout << sizeof(A2) << endl;// 8
cout << sizeof(C1) << endl;// 12
C1 c;
c.m_grand = 0;// 引入虚基类就不会有这种二义性问题了;
c.A1::m_grand = 0;
c.A2::m_grand = 0;
return 0;
}
// 当虚基类不存在的时候,如果Grand的构造函数有参数的话
// 由Grand的直接子类A1,A2来初始化,当有了虚基类之后,就由孙子类C1来负责初始化了
三.虚基类初探
class Grand
{
public:
};
class A1 :virtual public Grand
{
public:
};
class A2 :virtual public Grand
{
public:
};
class C1 :public A1, public A2
{
public:
};
int main()
{
cout << sizeof(Grand) << endl;// 1
cout << sizeof(A1) << endl; // 4 里面多了一个虚基类表指针
cout << sizeof(A2) << endl;// 4 里面多了一个虚基类表指针
cout << sizeof(C1) << endl;// 8 里面多了两个虚基类表指针,一个来自A1,一个来自A2;
A1 a1;// 虚基类表指针的指向:cc 7b 73 00
A2 a2;// 虚基类表指针的指向:d4 7b 73 00
// 注意:两个的虚基类表是不同的;
return 0;
}
class Grand
{
public:
int m_grand;
};
class A1 :virtual public Grand
{
public:
int a1;
};
class A2 :virtual public Grand
{
public:
int a2;
};
class C1 :public A1, public A2
{
public:
int c;
};
int main()
{
cout << sizeof(Grand) << endl;// 4 只包含自己的成员 m_grand;
cout << sizeof(A1) << endl; // 12 包含自己的成员 a1,还有继承的m_grand ,以及虚基类表指针;
cout << sizeof(A2) << endl;// 12 同上
cout << sizeof(C1) << endl;// 24 包含自己的成员 c,继承自A1的 a1和虚基类表指针,继承自A2的 a2和 虚基类表指针,以及m_grand;
return 0;
}
A1或者A2的成员内存布局(图解)
C的成员内存布局(图解):
四.结论:
虚基类的继承和有虚函数的普通类的继承的不同点:
a. 虚基类继承来的Grand类里面的成员都在最下面。
b.虚继承里面Grand类是没有虚基类指针的,但虚函数继承里面父类如果有虚函数的话,父类是存在虚函数指针的;
五.反思
为什么虚继承的Grand类成员总是在最下面呢?
我的内容可能写的比较潦草,如果需要笔记内容相对应的课程,请关注并私信我。