回顾类/对象大小计算
虚基类表
1.virtual base table
1.1本类地址与虚基类表指针地址的差
1.2虚基类地址与虚基类表指针地址的差
2.virtual base table pointer(vbptr)
#include <iostream>
using namespace std;
class BB
{
public:
int bb_;
};
class B2 : virtual public BB
{
public:
int b2_;
};
class B1 : virtual public BB
{
public:
int b1_;
};
class DD : public B1, public B2
{
public:
int dd_;
};
int main()
{
cout << sizeof(BB) << endl;
cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(DD) << endl;
B1 b1;
long** p;
cout << &b1 << endl;
cout << &b1.bb_ << endl;
cout << &b1.b1_ << endl;
p = (long**)&b1;
cout << p[0][0] << endl;
cout << p[0][1] << endl;
return 0;
}
#include <iostream>
using namespace std;
class BB
{
public:
int bb_;
};
class B2 : virtual public BB
{
public:
int b2_;
};
class B1 : virtual public BB
{
public:
int b1_;
};
class DD : public B1, public B2
{
public:
int dd_;
};
int main()
{
cout << sizeof(BB) << endl;
cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(DD) << endl;
B1 b1;
long** p;
cout << &b1 << endl;
cout << &b1.bb_ << endl;
cout << &b1.b1_ << endl;
p = (long**)&b1;
cout << p[0][0] << endl;
cout << p[0][1] << endl;
DD dd;
cout << &dd << endl;
cout << &dd.bb_ << endl;
cout << &dd.b1_ << endl;
cout << &dd.b2_ << endl;
cout << &dd.dd_ << endl;
p = (long**)ⅆ
cout << p[0][0] << endl;
cout << p[0][1] << endl;
cout << p[2][0] << endl;
cout << p[2][1] << endl;
return 0;
}
virtual:存在,共享,间接
|