class A size(8):
+---
0 | {vfptr}
4 | a
+---
A::$vftable@:
| &A_meta
| 0
0 | &A::pp
A::pp this adjustor: 0
class B size(8):
+---
0 | {vfptr}
4 | a
+---
B::$vftable@:
| &B_meta
| 0
0 | &B::pp
B::pp this adjustor: 0
class C size(20):
+---
| +--- (base class A)
0 | | {vfptr}
4 | | a
| +---
| +--- (base class B)
8 | | {vfptr}
12 | | a
| +---
16 | a
+---
C::$vftable@A@:
| &C_meta
| 0
0 | &C::pp
C::$vftable@B@:
| -8
0 | &thunk: this-=8; goto C::pp
C::pp this adjustor: 0
Class C
vftable
A.a
Vftable
B.a
C.a
Class A
&A.pp NULL
Class C
&B.pp NULL
为了方便,我把基类的类变量改为 a,b,c
#include
using namespace std;
class A
{
public:
int a;
virtual void pp()
{
}
A():a(10){}
};
class B
{
public:
int b;
virtual void pp(){};
B():b(20){}
};
class C:public A,public B
{
public:
int c;
virtual void pp()
{
}
C():c(30){}
};
void PrintMember(int *pI)
{
cout << *pI << endl;
}
void PrintVTAndMember(A *pD)
{
int *pRoot = (int*)pD;
int *pMB1 = pRoot +1; PrintMember(pMB1);
int *pMB2 = pRoot +3; PrintMember(pMB2);
int *pMB3 = pRoot +4; PrintMember(pMB3);
}
void TestVT()
{
A* pa = new C();
C* pc = dynamic_cast(pa);
pc->a = 100;
pc->b = 200;
pc->c = 300;
PrintVTAndMember(pc);
delete(pa);
}
int main()
{
TestVT();
system("Pause");
}
本文档为【虚基类】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。