关于虚拟继承和直接继承

VS2008,项目->属性->C/C++->命令行,附加选项:/d1reportAllClassLayout


编译下面的代码,可以很明显的看到两者的区别


1,直接继承,一直只有一个虚函数表指针,一个虚函数表:

class A       //父类
{
		int i;
public:
	virtual void aa(){};
};

class B:public  A    
{
		int j;
public:
		virtual void bb(){};
};

class C:public  B    
{
		int k;
public:
		virtual void cc(){};
};

class D:public  C    
{
		int l;
public:
		virtual void dd(){};
};
int main()
{
	cout<<sizeof(A)<<endl;
	cout<<sizeof(B)<<endl;
	cout<<sizeof(C)<<endl;
	cout<<sizeof(D)<<endl;
	return 0;
}
1> +---
1>class A size(8):
1> +---
1> 0 | {vfptr}
1> 4 | i
1> +---

1>class B size(12):
1> +---
1> | +--- (base class A)
1> 0 | | {vfptr}
1> 4 | | i
1> | +---
1> 8 | j
1> +---

1>class C size(16):
1> +---
1> | +--- (base class B)
1> | | +--- (base class A)
1> 0 | | | {vfptr}
1> 4 | | | i
1> | | +---
1> 8 | | j
1> | +---
1>12 | k
1> +---

1>class D size(20):
1> +---
1> | +--- (base class C)
1> | | +--- (base class B)
1> | | | +--- (base class A)
1> 0 | | | | {vfptr}
1> 4 | | | | i
1> | | | +---
1> 8 | | | j
1> | | +---
1>12 | | k
1> | +---
1>16 | l
1> +---

2.虚继承维护多个虚函数表,而且还有指向基类的虚函数表指针,如下:

#include<iostream>
using namespace std;

class A       //父类
{
		int i;
public:
	virtual void aa(){};
};

class B:public virtual A    
{
		int j;
public:
		virtual void bb(){};
};

class C:public virtual B    
{
		int k;
public:
		virtual void cc(){};
};

class D:public virtual C    
{
		int l;
public:
		virtual void dd(){};
};
int main()
{
	cout<<sizeof(A)<<endl;
	cout<<sizeof(B)<<endl;
	cout<<sizeof(C)<<endl;
	cout<<sizeof(D)<<endl;
	return 0;
}

1>class A size(8):
1> +---
1> 0 | {vfptr}
1> 4 | i
1> +---


1>class B size(20):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr}
1> 8 | j
1> +---
1> +--- (virtual base A)
1>12 | {vfptr}
1>16 | i
1> +---


1>class C size(32):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr}
1> 8 | k
1> +---
1> +--- (virtual base A)
1>12 | {vfptr}
1>16 | i
1> +---
1> +--- (virtual base B)
1>20 | {vfptr}
1>24 | {vbptr}
1>28 | j
1> +---

第三种情况:

class A       //父类
{
		int i;
public:
	virtual void aa(){};
};

class B:virtual public  A    
{
		int j;
public:
		virtual void bb(){};
};

class C:virtual public  A    
{
		int k;
public:
		virtual void cc(){};
};

class D:virtual public  C ,virtual public B   
{
		int l;
public:
		virtual void dd(){};
};
int main()
{
	cout<<sizeof(A)<<endl;
	cout<<sizeof(B)<<endl;
	cout<<sizeof(C)<<endl;
	cout<<sizeof(D)<<endl;
	return 0;
}
1>------ 已启动生成: 项目: 23, 配置: Debug Win32 ------
1>正在编译...
1>23.cpp
1> +---
1>class A size(8):
1> +---
1> 0 | {vfptr}
1> 4 | i
1> +---

1>class B size(20):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr}
1> 8 | j
1> +---
1> +--- (virtual base A)
1>12 | {vfptr}
1>16 | i
1> +---

1>class C size(20):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr}
1> 8 | k
1> +---
1> +--- (virtual base A)
1>12 | {vfptr}
1>16 | i
1> +---

1>class D size(44):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr}
1> 8 | l
1> +---
1> +--- (virtual base A)
1>12 | {vfptr}
1>16 | i
1> +---
1> +--- (virtual base C)
1>20 | {vfptr}
1>24 | {vbptr}
1>28 | k
1> +---
1> +--- (virtual base B)
1>32 | {vfptr}
1>36 | {vbptr}
1>40 | j
1> +---

第四种情况:

class A       //父类
{
		int i;
public:
	virtual void aa(){};
};

class B:public  A    
{
		int j;
public:
		virtual void bb(){};
};

class C:public  A    
{
		int k;
public:
		virtual void cc(){};
};

class D:public  C ,public B   
{
		int l;
public:
		virtual void dd(){};
};
int main()
{
	cout<<sizeof(A)<<endl;
	cout<<sizeof(B)<<endl;
	cout<<sizeof(C)<<endl;
	cout<<sizeof(D)<<endl;
	return 0;
}

1> +---
1>class A size(8):
1> +---
1> 0 | {vfptr}
1> 4 | i
1> +---

1>class B size(12):
1> +---
1> | +--- (base class A)
1> 0 | | {vfptr}
1> 4 | | i
1> | +---
1> 8 | j
1> +---

1>class C size(12):
1> +---
1> | +--- (base class A)
1> 0 | | {vfptr}
1> 4 | | i
1> | +---
1> 8 | k
1> +---

1>class D size(28):
1> +---
1> | +--- (base class C)
1> | | +--- (base class A)
1> 0 | | | {vfptr}
1> 4 | | | i
1> | | +---
1> 8 | | k
1> | +---
1> | +--- (base class B)
1> | | +--- (base class A)
1>12 | | | {vfptr}
1>16 | | | i
1> | | +---
1>20 | | j
1> | +---
1>24 | l
1> +---



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值