对象内存布局 (9)
来源:互联网 发布:美洲文明知乎 编辑:程序博客网 时间:2024/05/16 09:05
前篇:http://blog.csdn.net/pathuang68/archive/2009/04/23/4102006.aspx
假定多层继承的各类之间的关系如下图。假定派生类不override基类的虚函数,即Base2不override Base1中声明的虚函数vfBase1(),Base3不override Base2中声明的虚函数vfBase2(),Derived不override Base3中声明的虚函数vfBase3()。
代码如下:
#include <iostream>
using namespace std;
class Base1
{
public:
int m_base1;
inline virtual void vfBase1_1()
{
cout << "This is in Base1::vfBase1_1()" << endl;
}
};
class Base2 : public Base1
{
public:
int m_base2;
inline virtual void vfBase2_1()
{
cout << "This is in Base2::vfBase2_1()" << endl;
}
};
class Base3 : public Base2
{
public:
int m_Base3;
inline virtual void vfBase3_1()
{
cout << "This is in Base3::vfBase3_1()" << endl;
}
};
class Derived : public Base3
{
public:
int m_derived;
inline virtual void fd()
{
cout << "This is in Derived::fd()" << endl;
}
};
typedef void (*VFun)(void);
template<typename T>
VFun virtualFunctionPointer(T* b, int i)
{
return (VFun)(*((int*)(*(int*)b) + i));
}
int main(void)
{
Derived d;
cout << "The size of Derived object = /t" << sizeof(Derived) << endl;
cout << endl;
cout << "1st virtual function table: " << endl;
int i = 0;
while(virtualFunctionPointer(&d, i))
{
VFun pVF = virtualFunctionPointer(&d, i++);
pVF();
}
return 0;
}
运行结果:
Derived对象的memory layout图解如下:
后篇:http://blog.csdn.net/pathuang68/archive/2009/04/23/4103627.aspx
- 对象内存布局 (9)
- 对象内存布局 (9)
- 对象内存布局 (9)
- C++内存对象布局
- c++对象,内存布局
- c++对象内存布局
- 对象内存布局 (1)
- 对象内存布局 (2)
- 对象内存布局 (3)
- 对象内存布局 (4)
- 对象内存布局 (5)
- 对象内存布局 (6)
- 对象内存布局 (7)
- 对象内存布局 (8)
- 对象内存布局 (10)
- 对象内存布局 (11)
- 对象内存布局 (12)
- 对象内存布局 (13)
- DeviceNet总线技术综述
- Thinking in java 4th Edition 习题答案全
- windows版ncurses库
- 善用GridView控件的HyperLinkField数据行
- Google Analytics API发布
- 对象内存布局 (9)
- 一些好玩的测试
- JSP的那些事儿(2)----DWR2.0 的配置和使用
- C# DBF文件转ACCESS数据库
- 使用 ToolHelp32 库枚举进程
- asp调用DLL或com组件的简述及网上摘例
- DeviceNet概要
- wmplayer参数说明
- C#窗体贴边自动隐藏组件 By Red_angelX