对象内存布局 (8)
2009-04-23 01:56
239 查看
前篇:http://blog.csdn.net/pathuang68/archive/2009/04/23/4102003.aspx
在内存对象布局 (5)的代码中,在Derived类中将三个基类中的虚函数分别覆盖一个,即分别覆盖Base1中声明的vfBase1_1(),Base2中声明的vfBase2_1()以及Base3中声明的vfBase3_1()。保持其他代码不变,修改后的Derived代码如下:
class Derived : public Base1, public Base2, public Base3
{
public:
int m_derived;
inline virtual void fd()
{
cout << "This is in Derived::fd()" << endl;
}
inline void vfBase1_1()
{
cout << "This is in Derived::vfBase1_1()" << endl;
}
inline void vfBase2_1()
{
cout << "This is in Derived::vfBase2_1()" << endl;
}
inline void vfBase3_1()
{
cout << "This is in Derived::vfBase3_1()" << endl;
}
};
运行结果如下:
Derived对象的memory layout图解如下:
Derived中覆盖的虚函数,分别出现在三个不同的虚函数表中,而且分别代替个基类的原虚函数的位置,即:
第一个虚函数表中,Derived::vfBase1_1()代替了Base::vfBase1_1()的位置,Base::vfBase1_1()不再在虚函数表中出现;
第二个虚函数表中,Derived::vfBase2_1()代替了Base::vfBase2_1()的位置,Base::vfBase2_1()不再在虚函数表中出现;
第三个虚函数表中,Derived::vfBase3_1()代替了Base::vfBase3_1()的位置,Base::vfBase3_1()不再在虚函数表中出现;
在Derived中自己定义的虚函数,总是处在第一个虚函数表的最后一项的位置。
在内存对象布局 (5)的代码中,在Derived类中将三个基类中的虚函数分别覆盖一个,即分别覆盖Base1中声明的vfBase1_1(),Base2中声明的vfBase2_1()以及Base3中声明的vfBase3_1()。保持其他代码不变,修改后的Derived代码如下:
class Derived : public Base1, public Base2, public Base3
{
public:
int m_derived;
inline virtual void fd()
{
cout << "This is in Derived::fd()" << endl;
}
inline void vfBase1_1()
{
cout << "This is in Derived::vfBase1_1()" << endl;
}
inline void vfBase2_1()
{
cout << "This is in Derived::vfBase2_1()" << endl;
}
inline void vfBase3_1()
{
cout << "This is in Derived::vfBase3_1()" << endl;
}
};
运行结果如下:
Derived对象的memory layout图解如下:
Derived中覆盖的虚函数,分别出现在三个不同的虚函数表中,而且分别代替个基类的原虚函数的位置,即:
第一个虚函数表中,Derived::vfBase1_1()代替了Base::vfBase1_1()的位置,Base::vfBase1_1()不再在虚函数表中出现;
第二个虚函数表中,Derived::vfBase2_1()代替了Base::vfBase2_1()的位置,Base::vfBase2_1()不再在虚函数表中出现;
第三个虚函数表中,Derived::vfBase3_1()代替了Base::vfBase3_1()的位置,Base::vfBase3_1()不再在虚函数表中出现;
在Derived中自己定义的虚函数,总是处在第一个虚函数表的最后一项的位置。
相关文章推荐
- C++ 对象的内存布局(上)
- JVM——深入分析对象的内存布局
- c++对象内存模型【内存布局】
- OC对象的内存布局
- JVM--第二章--深入理解对象的创建、内存布局、访问定位
- JVM——深入分析对象的内存布局
- C++ 对象的内存布局上
- C++对象模型之简述C++对象的内存布局
- java对象的内存布局 以及 对象访问定位方法
- C++虚继承中的对象内存布局
- c++对象内存模型【内存布局】
- C++ 对象的内存布局(下)
- java对象的内存布局(一):计算java对象占用的内存空间以及java object layout
- C++对象内存布局--③测试多继承中派生类的虚函数在哪一张虚函数表中
- C++对象内存布局--⑩GCC编译器--虚拟继承--菱形继承
- C++ 对象的内存布局—— 虚继承下的虚函数
- 对象内存布局 (4)
- C++ 对象的内存布局(上)
- 对象内存布局 (1)
- 对象内存布局 (12)