续:
C++多态和对象的内存布局(一)
C++多态和对象的内存布局(二)
C++多态和对象的内存布局(三)
14. 普通钻石继承
程序输出:
sizeof(Ba_1) = 12
sizeof(Derive_1) = 20
sizeof(Derive_2) = 20
sizeof(Derive_3) = 48 为什么这么大??
Base1内存布局:
1> class Base_1 size(12):
1> +---
1> 0 | {vfptr}
1> 4 | base1_1
1> 8 | base1_2
1> +---
Base1虚函数表:
1> Base_1::$vftable@:
1> | &Base_1_meta
1> | 0
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1> Base_1::base_1_fun_1 this adjustor: 0
1> Base_1::base_1_fun_2 this adjustor: 0
Derive1内存布局:
1> class Derive_1 size(20):
1> +---
1> 0 | +--- (base class Base_1)
1> 0 | | {vfptr}
1> 4 | | base1_1
1> 8 | | base1_2
1> | +---
1> 12 | derive1_1
1> 16 | derive1_2
1> +---
Derive1虚函数表:
1> Derive_1::$vftable@:
1> | &Derive_1_meta
1> | 0
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1> 2 | &Derive_1::derive_1_fun1
1> 3 | &Derive_1::derive_1_fun2
1> Derive_1::derive_1_fun1 this adjustor: 0
1> Derive_1::derive_1_fun2 this adjustor: 0
Derive2内存布局:
1> class Derive_2 size(20):
1> +---
1> 0 | +--- (base class Base_1)
1> 0 | | {vfptr}
1> 4 | | base1_1
1> 8 | | base1_2
1> | +---
1> 12 | derive2_1
1> 16 | derive2_2
1> +---
Derive2虚函数表:
1> Derive_2::$vftable@:
1> | &Derive_2_meta
1> | 0
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1> 2 | &Derive_2::derive_2_fun1
1> 3 | &Derive_2::derive_2_fun2
1> Derive_2::derive_2_fun1 this adjustor: 0
1> Derive_2::derive_2_fun2 this adjustor: 0
Derive3内存布局:
1> class Derive_3 size(48):
1> +---
1> 0 | +--- (base class Derive_1)
1> 0 | | +--- (base class Base_1) 共有基类Base_1内存
1> 0 | | | {vfptr}
1> 4 | | | base1_1
1> 8 | | | base1_2
1> | | +---
1> 12 | | derive1_1
1> 16 | | derive1_2
1> | +---
1> 20 | +--- (base class Derive_2)
1> 20 | | +--- (base class Base_1) 又有一份共有基类Base_1内存 , 跟我们想要的不一样?
1> 20 | | | {vfptr}
1> 24 | | | base1_1
1> 28 | | | base1_2
1> | | +---
1> 32 | | derive2_1
1> 36 | | derive2_2
1> | +---
1> 40 | derive3_1
1> 44 | derive3_2
1> +---
Derive3虚函数表Derive_1:
1> Derive_3::$vftable@Derive_1@:
1> | &Derive_3_meta
1> | 0
1> 0 | &Base_1::base_1_fun_1 Base_1虚函数
1> 1 | &Base_1::base_1_fun_2 Base_1虚函数
1> 2 | &Derive_1::derive_1_fun1
1> 3 | &Derive_1::derive_1_fun2
1> 4 | &Derive_3::derive_3_fun1
1> 5 | &Derive_3::derive_3_fun2
Derive3虚函数表Derive_2:
1> Derive_3::$vftable@Derive_2@:
1> | -20
1> 0 | &Base_1::base_1_fun_1 Base_1虚函数 重复
1> 1 | &Base_1::base_1_fun_2 Base_1虚函数 重复
1> 2 | &Derive_2::derive_2_fun1
1> 3 | &Derive_2::derive_2_fun2
1> Derive_3::derive_3_fun1 this adjustor: 0
1> Derive_3::derive_3_fun2 this adjustor: 0
class Base_1
{
public:
int base1_1;
int base1_2;
virtual void base_1_fun_1() {}
virtual void base_1_fun_2() {}
};
/************************
class Base_2
{
public:
int base2_1;
int base2_2;
virtual void base_2_fun_1() {}
virtual void base_2_fun_2() {}
};
***************************/
// 多继承
class Derive_1 : public Base_1
{
public:
int derive1_1;
int derive1_2;
// Derive_1的虚函数
virtual void derive_1_fun1() {}
virtual void derive_1_fun2() {}
};
// 多继承
class Derive_2 : public Base_1
{
public:
int derive2_1;
int derive2_2;
// Derive_1的虚函数
virtual void derive_2_fun1() {}
virtual void derive_2_fun2() {}
};
// 多继承
class Derive_3 : public Derive_1, public Derive_2
{
public:
int derive3_1;
int derive3_2;
// Derive_1的虚函数
virtual void derive_3_fun1() {}
virtual void derive_3_fun2() {}
};
int main()
{
cout << endl << "14. 普通钻石继承" << endl;
cout << "sizeof(Base_1) = " << sizeof(Base_1) << endl;
//cout << "sizeof(Base_2) = " << sizeof(Base_2) << endl;
cout << "sizeof(Derive_1) = " << sizeof(Derive_1) << endl;
cout << "sizeof(Derive_2) = " << sizeof(Derive_2) << endl;
cout << "sizeof(Derive_3) = " << sizeof(Derive_3) << endl << endl;
return 0;
}
15.钻石继承-带有virtual关键字
程序输出:
sizeof(Base_1) = 12
sizeof(Derive_1) = 24
sizeof(Derive_2) = 24
sizeof(Derive_3) = 44 比第14条少了4个字节
1> class Base_1 size(12):
1> +---
1> 0 | {vfptr}
1> 4 | base1_1
1> 8 | base1_2
1> +---
1>
1> Base_1::$vftable@:
1> | &Base_1_meta
1> | 0
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1>
1> Base_1::base_1_fun_1 this adjustor: 0
1> Base_1::base_1_fun_2 this adjustor: 0
1>
1> class Derive_1 size(28):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr} 多了一个指针:vbptr指的是虚基类表指针virtual base table pointer
1> 8 | derive1_1
1> 12 | derive1_2
1> +---
1> +--- (virtual base Base_1)
1> 16 | {vfptr}
1> 20 | base1_1
1> 24 | base1_2
1> +---
1>
1> Derive_1::$vftable@Derive_1@:
1> | &Derive_1_meta
1> | 0
1> 0 | &Derive_1::derive_1_fun1
1> 1 | &Derive_1::derive_1_fun2
1>
1> Derive_1::$vbtable@:
1> 0 | -4
1> 1 | 12 (Derive_1d(Derive_1+4)Base_1)
1>
1> Derive_1::$vftable@Base_1@:
1> | -16
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1>
1> Derive_1::derive_1_fun1 this adjustor: 0
1> Derive_1::derive_1_fun2 this adjustor: 0
1> vbi: class offset o.vbptr o.vbte fVtorDisp 表示从当前类Derive_1 偏移16可以找到基类Base_1的内容
1> Base_1 16 4 4 0
1>
1> class Derive_2 size(28):
1> +---
1> 0 | {vfptr}
1> 4 | {vbptr} 多了一个指针:vbptr指的是虚基类表指针virtual base table pointer
1> 8 | derive2_1
1> 12 | derive2_2
1> +---
1> +--- (virtual base Base_1)
1> 16 | {vfptr}
1> 20 | base1_1
1> 24 | base1_2
1> +---
1>
1> Derive_2::$vftable@Derive_2@:
1> | &Derive_2_meta
1> | 0
1> 0 | &Derive_2::derive_2_fun1
1> 1 | &Derive_2::derive_2_fun2
1>
1> Derive_2::$vbtable@:
1> 0 | -4
1> 1 | 12 (Derive_2d(Derive_2+4)Base_1)
1>
1> Derive_2::$vftable@Base_1@:
1> | -16
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1>
1> Derive_2::derive_2_fun1 this adjustor: 0
1> Derive_2::derive_2_fun2 this adjustor: 0
1> vbi: class offset o.vbptr o.vbte fVtorDisp 表示从当前类Derive_2 偏移16可以找到基类Base_1的内容
1> Base_1 16 4 4 0
1>
1> class Derive_3 size(52):
1> +---
1> 0 | +--- (base class Derive_1) 表示从当前类Derive_3 偏移0可以找到基类Derive_1的内容
1> 0 | | {vfptr}
1> 4 | | {vbptr} 多了一个指针:vbptr指的是虚基类表指针virtual base table pointer
1> 8 | | derive1_1
1> 12 | | derive1_2
1> | +---
1> 16 | +--- (base class Derive_2) 表示从当前类Derive_3 偏移16可以找到基类Derive_2的内容
1> 16 | | {vfptr}
1> 20 | | {vbptr} 多了一个指针:vbptr指的是虚基类表指针virtual base table pointer
1> 24 | | derive2_1
1> 28 | | derive2_2
1> | +---
1> 32 | derive3_1
1> 36 | derive3_2
1> +---
1> +--- (virtual base Base_1) 只有一份共有基类Base_1 的内容
1> 40 | {vfptr} 表示从当前类Derive_3 偏移40可以找到共有基类Base_1的内容
1> 44 | base1_1
1> 48 | base1_2
1> +---
1>
1> Derive_3::$vftable@Derive_1@:
1> | &Derive_3_meta
1> | 0
1> 0 | &Derive_1::derive_1_fun1
1> 1 | &Derive_1::derive_1_fun2
1> 2 | &Derive_3::derive_3_fun1 还是一样追加到了第一个拥有虚函数表的末尾
1> 3 | &Derive_3::derive_3_fun2
1>
1> Derive_3::$vftable@Derive_2@:
1> | -16
1> 0 | &Derive_2::derive_2_fun1
1> 1 | &Derive_2::derive_2_fun2
1>
1> Derive_3::$vbtable@Derive_1@:
1> 0 | -4
1> 1 | 36 (Derive_3d(Derive_1+4)Base_1)
1>
1> Derive_3::$vbtable@Derive_2@:
1> 0 | -4
1> 1 | 20 (Derive_3d(Derive_2+4)Base_1)
1>
1> Derive_3::$vftable@Base_1@:
1> | -40
1> 0 | &Base_1::base_1_fun_1
1> 1 | &Base_1::base_1_fun_2
1>
1> Derive_3::derive_3_fun1 this adjustor: 0
1> Derive_3::derive_3_fun2 this adjustor: 0
1> vbi: class offset o.vbptr o.vbte fVtorDisp
1> Base_1 40 4 4 0 表示从当前类Derive_3 偏移40可以找到共有基类Base_1的内容
class Base_1
{
public:
int base1_1;
int base1_2;
virtual void base_1_fun_1() {}
virtual void base_1_fun_2() {}
};
/************************
class Base_2
{
public:
int base2_1;
int base2_2;
virtual void base_2_fun_1() {}
virtual void base_2_fun_2() {}
};
***************************/
// 多继承
class Derive_1 : virtual public Base_1
{
public:
int derive1_1;
int derive1_2;
// Derive_1的虚函数
virtual void derive_1_fun1() {}
virtual void derive_1_fun2() {}
};
// 多继承
class Derive_2 : virtual public Base_1
{
public:
int derive2_1;
int derive2_2;
// Derive_1的虚函数
virtual void derive_2_fun1() {}
virtual void derive_2_fun2() {}
};
// 多继承
class Derive_3 : public Derive_1, public Derive_2
{
public:
int derive3_1;
int derive3_2;
// Derive_1的虚函数
virtual void derive_3_fun1() {}
virtual void derive_3_fun2() {}
};
int main()
{
cout << endl << "15.钻石继承-带有virtual关键字" << endl;
cout << "sizeof(Base_1) = " << sizeof(Base_1) << endl;
//cout << "sizeof(Base_2) = " << sizeof(Base_2) << endl;
cout << "sizeof(Derive_1) = " << sizeof(Derive_1) << endl;
cout << "sizeof(Derive_2) = " << sizeof(Derive_2) << endl;
cout << "sizeof(Derive_3) = " << sizeof(Derive_3) << endl << endl;
return 0;
}
未完待续: