#include<iostream>
using namespace std;
//sizeof(A) = 4;
//sizoef(B) = 12(变量a,b共8字节,虚基类表指针4 )
//sizeof(C) = 12(变量a,c共8字节,虚基类表指针4 )
//sizeof(D) = 24(24,变量a,b,c,d共16,B的虚基类指针4,C的虚基类指针)
class A{public: int a;};
class B : virtual public A{public: int b;};
class C : virtual public A{public: int c;};
class D : public B, public C{public: int d;};
void main()
{
D d; cout << "sizeof(D) = " << sizeof(D) << endl;
d.a = 10; d.b = d.c = 20; d.d = 30;
int* pInt = (int*)(&d);
for(int i = 0; i < sizeof(D)/sizeof(int); i++){
if(i == 0 || i == 2)
cout << "[" << pInt << "] : " << *pInt << "(" << *((int*)(*pInt)) << ")" << endl;
else
cout << "[" << pInt << "] : " << *pInt << endl;
pInt++;
}
}
C++简单菱形继承
最新推荐文章于 2022-12-11 09:48:43 发布