C++:类的内存布局


建议先了解 C++ 继承与多态的相关知识,再来阅读。也可以看我之前写过的: C++:继承C++:多态,这两篇文章。

1、虚的含义

虚的含义是存在、间接、共享,在虚函数和虚拟继承中,其含义分别解释为

虚函数

  • 存在:虚函数是存在的

  • 间接:虚函数必须通过虚函数表在运行期间调用

  • 共享:基类指针会共享被派生类重定义的虚函数

虚拟继承

  • 存在:虚继承体系和虚基类确实存在

  • 间接:当访问虚基类的成员时必须通过虚基表间接完成

  • 共享:虚基类会在虚继承体系中被共享,而不会出现多份拷贝

虚函数的实现机制

  • 虚函数指针 vfptr:指向虚函数表。位于派生类内存空间的起始。
  • 虚函数表:存放虚函数入口地址。

虚拟继承的实现机制

  • 虚基指针 vbptr:指向虚基表。虚基类位于派生类内存空间的末尾。
  • 虚基表:存放虚基类对象的地址。

2、单基继承

2.1、单继承

基类位于派生类存储空间的起始。

在这里插入图片描述

2.2、单虚继承

虚基类位于派生类存储空间的末尾。

在这里插入图片描述

1>class B	size(16):
1>	+---          
1> 0	| {vbptr} // 派生类的虚基指针
1> 4	| _ib               
1>	+---
1>	+--- (virtual base A) // 虚基类位于派生类存储空间的最末尾
1> 8	| {vfptr} 		 // 虚基类的虚函数指针
1>12	| _ia
1>	+---

// 派生类的虚基表
1>B::$vbtable@: 
1> 0	| 0  		  // 距离派生类对象首地址的偏移
1> 1	| 8 (Bd(B+0)A) // 距离虚基类对象的首地址的偏移
1>

// 虚基类的虚函数表
1>B::$vftable@:  // 虚函数表,存放基类虚函数的地址
1>	    | -8     // 距离派生类对象首地址的偏移
1> 0	| &B::f  // 指向虚函数的地址

2.3、单虚继承 + 虚函数

派生类拥有自己的虚函数

在这里插入图片描述

1>class B	size(20):
1>	+---
1> 0	| {vfptr} // 派生类的虚函数指针
1> 4	| {vbptr} // 派生类的虚基指针
1> 8	| _ib
1>	+---
1>	+--- (virtual base A) // 虚基类
1>12	| {vfptr} 
1>16	| _ia
1>	+---
// 派生类的虚函数表
1>B::$vftable@B@: 
1>		| &B_meta
1>		|  0
1> 0	| &B::fb2 // 派生类自定义的虚函数
1>
// 虚基类的虚基表
1>B::$vbtable@:  
1> 0	| -4
1> 1	| 8 (Bd(B+4)A) 
1>
// 虚基类的虚函数表
1>B::$vftable@A@: 
1>	    | -12
1> 0	| &B::f    

2.4、测试代码

#pragma vtordisp(off)
#include <iostream>
using std::cout;
using std::endl;

class A {
public:
	A() : _ia(10) {}
	virtual void f() 
    { cout << "A::f()" << endl; }
private:
	int _ia;
};

// 测试代码:在类B上修改相应的代码
// 测试1:单继承,不带虚函数,注释掉所有的virtual关键字
// 测试2:单虚拟继承,不带虚函数,注释掉类内的所有virtual关键字
// 测试3:单虚拟继承,带虚函数,打开类内的virtual关键字
class B: virtual public A{
public:
	B() : _ib(20) {}
	void fb()
	{ cout << "A::fb()" << endl; }
	virtual void f()
	{ cout << "B::f()" << endl; }
	virtual void fb2()
    { cout << "B::fb2()" << endl;}

private:
	int _ib;
};

int main() {
	cout << sizeof(A) << endl;
	cout << sizeof(B) << endl;
	B b;

	return 0;
}

3、多基继承

3.1、多继承 + 虚函数

  • 每个基类都有自己的虚函数表
  • 内存布局中,其基类的布局按照基类被声明时的顺序进行排列
  • 派生类如果有自己的虚函数,会被加入到第一个虚函数表之中
  • 派生类会覆盖基类的虚函数,只有第一个虚函数表中存放的是虚函数的入口地址。其他的虚函数表中存放的是一条跳转指令,指向当前派生类对象的起始地址。

测试:多基继承 + 基类虚函数 + 派生类自定义虚函数

在这里插入图片描述

1>class Derived	size(28):
1>	+---
1> 0	| +--- (base class Base1)
1> 0	| | {vfptr}
1> 4	| | _iBase1
1>	| +---
1> 8	| +--- (base class Base2)
1> 8	| | {vfptr}
1>12	| | _iBase2
1>	| +---
1>16	| +--- (base class Base3)
1>16	| | {vfptr}
1>20	| | _iBase3
1>	| +---
1>24	| _iDerived
1>	+---
    
1>Derived::$vftable@Base1@:
1>	    | &Derived_meta
1>	    |  0
1> 0	| &Derived::f  // 派生类重写基类的虚函数
1> 1	| &Base1::g
1> 2	| &Base1::h
1> 3	| &Derived::g1 // 派生类自定义的虚函数
    
1>Derived::$vftable@Base2@:
1>	| -8
1> 0	| &thunk: this-=8; goto Derived::f  // 跳转指令
1> 1	| &Base2::g
1> 2	| &Base2::h
    
1>Derived::$vftable@Base3@:
1>	    | -16
1> 0	| &thunk: this-=16; goto Derived::f // 跳转指令
1> 1	| &Base3::g
1> 2	| &Base3::h

3.2、虚拟多继承 + 虚函数

测试:多基继承 + 基类虚函数 + 派生类自定义虚函数

注:若有继承的基类,则复用基类的第一个虚函数表;若没有继承的基类,如本例中只有虚基类,则派生类自定义虚函数表。

在这里插入图片描述

1>class Derived	size(36):
1>	+---
1> 0	| {vfptr}  // 虚函数指针
1> 4	| {vbptr}  // 虚基指针
1> 8	| _iDerived
1>	+---
1>	+--- (virtual base Base1)
1>12	| {vfptr}
1>16	| _iBase1
1>	+---
1>	+--- (virtual base Base2)
1>20	| {vfptr}
1>24	| _iBase2
1>	+---
1>	+--- (virtual base Base3)
1>28	| {vfptr}
1>32	| _iBase3
1>	+---
    
// 派生类的虚函数表
1>Derived::$vftable@Derived@:
1>	| &Derived_meta
1>	|  0
1> 0	| &Derived::g1
    
// 派生类的虚基表
1>Derived::$vbtable@: 
1> 0	| -4  
1> 1	| 8 (Derivedd(Derived+4)Base1)  // 虚基类 base1 的地址
1> 2	| 16 (Derivedd(Derived+4)Base2) // 虚基类 base2 的地址
1> 3	| 24 (Derivedd(Derived+4)Base3) // 虚基类 base3 的地址
    
1>Derived::$vftable@Base1@:
1>	| -12
1> 0	| &Derived::f  // 派生类重写虚基类的虚函数,override
1> 1	| &Base1::g
1> 2	| &Base1::h
    
1>Derived::$vftable@Base2@:
1>	| -20
1> 0	| &thunk: this-=8; goto Derived::f
1> 1	| &Base2::g
1> 2	| &Base2::h
    
1>Derived::$vftable@Base3@:
1>	| -28
1> 0	| &thunk: this-=16; goto Derived::f
1> 1	| &Base3::g
1> 2	| &Base3::h

3.3、测试代码

#include <iostream>

using std::cout;
using std::endl;

class Base1 {
public:
	Base1() : _iBase1(10) {}

	virtual void f()
    { cout << "Base1::f()" << endl; }

	virtual void g()
	{ cout << "Base1::g()" << endl; }

	virtual void h()
	{ cout << "Base1::h()" << endl; }
private:
	int _iBase1;
};

class Base2{
public:
	Base2() : _iBase2(100) {}

	virtual void f()
	{ cout << "Base2::f()" << endl; }

	virtual void g()
	{ cout << "Base2::g()" << endl; }

	virtual void h()
	{ cout << "Base2::h()" << endl; }
private:
	int _iBase2;
};

class Base3 {
public:
	Base3() : _iBase3(1000) {}

	virtual void f()
	{ cout << "Base3::f()" << endl; }

	virtual void g()
	{ cout << "Base3::g()" << endl; }

	virtual void h()
	{ cout << "Base3::h()" << endl; }
private:
	int _iBase3;
};


class Derived
	: /*virtual*/ public Base1
	, /*virtual*/ public Base2
	, /*virtual*/ public Base3
{
public:
	Derived() : _iDerived(10000) {}
	void f()
	{ cout << "Derived::f()" << endl; }

	virtual void g1()
	{ cout << "Derived::g1()" << endl; }

private:
	int _iDerived;
};

int main(void) {
	Derived d;
	Base2* pBase2 = &d;
	Base3* pBase3 = &d;
	Derived* pDerived = &d;

	pBase2->f();
	cout << "sizeof(d) = " << sizeof(d) << endl;

	cout << "&Derived = " << &d << endl;   
	cout << "pBase2 = " << pBase2 << endl; 
	cout << "pBase3 = " << pBase3 << endl; 

	return 0;
}

4、菱形继承

4.1、菱形继承

基类 B 出现存储二义性,拷贝了两份相同的。

在这里插入图片描述

class D	size(48):
1>	+---
1> 0	| +--- (base class B1)
1> 0	| | +--- (base class B)
1> 0	| | | {vfptr}
1> 4	| | | _ib
1> 8	| | | _cb 
1>  	| | | <alignment member> (size=3) // 内存对齐
1>	| | +---
1>12	| | _ib1
1>16	| | _cb1
1>  	| | <alignment member> (size=3)
1>	| +---
1>20	| +--- (base class B2)
1>20	| | +--- (base class B)
1>20	| | | {vfptr}
1>24	| | | _ib
1>28	| | | _cb
1>  	| | | <alignment member> (size=3)
1>	| | +---
1>32	| | _ib2
1>36	| | _cb2
1>  	| | <alignment member> (size=3)
1>	| +---
1>40	| _id
1>44	| _cd
1>  	| <alignment member> (size=3)
1>	+---
1>D::$vftable@B1@:
1>	| &D_meta
1>	|  0
1> 0	| &D::f
1> 1	| &B::Bf
1> 2	| &D::f1
1> 3	| &B1::Bf1
1> 4	| &D::Df
1>D::$vftable@B2@:
1>	| -20
1> 0	| &thunk: this-=20; goto D::f
1> 1	| &B::Bf
1> 2	| &D::f2
1> 3	| &B2::Bf2

4.2、菱形虚拟继承

虚拟继承后,基类 B 只拷贝了一次。

在这里插入图片描述

1>class D	size(52):
1>	+---
1> 0	| +--- (base class B1)
1> 0	| | {vfptr}
1> 4	| | {vbptr}
1> 8	| | _ib1
1>12	| | _cb1
1>  	| | <alignment member> (size=3)
1>	| +---
1>16	| +--- (base class B2)
1>16	| | {vfptr}
1>20	| | {vbptr}
1>24	| | _ib2
1>28	| | _cb2
1>  	| | <alignment member> (size=3)
1>	| +---
1>32	| _id
1>36	| _cd
1>  	| <alignment member> (size=3)
1>	+---
1>	+--- (virtual base B)
1>40	| {vfptr}
1>44	| _ib
1>48	| _cb
1>  	| <alignment member> (size=3)
1>	+---
    
1>D::$vftable@B1@:
1>	| &D_meta
1>	|  0
1> 0	| &D::f1
1> 1	| &B1::Bf1
1> 2	| &D::Df
1>D::$vftable@B2@:
1>	| -16
1> 0	| &D::f2
1> 1	| &B2::Bf2
1>D::$vbtable@B1@:
1> 0	| -4  
1> 1	| 36 (Dd(B1+4)B) 
1>D::$vbtable@B2@:
1> 0	| -4
1> 1	| 20 (Dd(B2+4)B)
1>D::$vftable@B@:
1>	| -40
1> 0	| &D::f
1> 1	| &B::Bf

4.3、测试代码

#include <iostream>
using std::cout;
using std::endl;

class B {
public:
	B() : _ib(10), _cb('B') {}

	virtual void f()
	{ cout << "B::f()" << endl; }

	virtual void Bf()
	{ cout << "B::Bf()" << endl; }

private:
	int _ib;
	char _cb;
};

class B1 : virtual public B {
public:
	B1() : _ib1(100), _cb1('1') {}

	virtual void f()
	{ cout << "B1::f()" << endl; }

#if 1
	virtual void f1()
	{ cout << "B1::f1()" << endl; }
	virtual void Bf1()
	{ cout << "B1::Bf1()" << endl; }
#endif

private:
	int _ib1;
	char _cb1;
};

class B2 : virtual public B {
public:
	B2() : _ib2(1000), _cb2('2') {}

	virtual void f()
	{ cout << "B2::f()" << endl; }
#if 1
	virtual void f2()
	{ cout << "B2::f2()" << endl; }
	virtual void Bf2()
	{ cout << "B2::Bf2()" << endl; }
#endif
private:
	int _ib2;
	char _cb2;
};

class D : public B1, public B2 {
public:
	D() : _id(10000), _cd('3') {}

	virtual void f()
	{ cout << "D::f()" << endl; }

#if 1
	virtual void f1()
	{ cout << "D::f1()" << endl; }
	virtual void f2()
	{ cout << "D::f2()" << endl; }

	virtual void Df()
	{ cout << "D::Df()" << endl; }
#endif
private:
	int _id;
	char _cd;
};

int main(void) {
	D d;
	cout << sizeof(d) << endl;
	return 0;
}

5、效率分析

实际使用过程中,减少使用多重继承、虚拟继承,因为对内存消耗大。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值