C++ 对象的内存布局(单继承)
来源:互联网 发布:图图名片设计软件 编辑:程序博客网 时间:2024/05/10 06:19
原文:http://blog.csdn.net/haoel/article/details/3081328
关于一般单继承C++对象的内存布局,我们假设有如下所示的一个继承关系:
请注意,在这个继承关系中,父类,子类,孙子类都有自己的一个成员变量。而了类覆盖了父类的f()方法,孙子类覆盖了子类的g_child()及其超类的f()。
我们的源程序如下所示:
class Parent {public:int iparent;Parent ():iparent (10) {}virtual void f() { cout << " Parent::f()" << endl; }virtual void g() { cout << " Parent::g()" << endl; }virtual void h() { cout << " Parent::h()" << endl; }};class Child : public Parent {public:int ichild;Child():ichild(100) {}virtual void f() { cout << "Child::f()" << endl; }virtual void g_child() { cout << "Child::g_child()" << endl; }virtual void h_child() { cout << "Child::h_child()" << endl; }};class GrandChild : public Child{public:int igrandchild;GrandChild():igrandchild(1000) {}virtual void f() { cout << "GrandChild::f()" << endl; }virtual void g_child() { cout << "GrandChild::g_child()" << endl; }virtual void h_grandchild() { cout << "GrandChild::h_grandchild()" << endl; }};
我们使用以下程序作为测试程序:(下面程序中,我使用了一个int** pVtab 来作为遍历对象内存布局的指针,这样,我就可以方便地像使用数组一样来遍历所有的成员包括其虚函数表了,在后面的程序中,我也是用这样的方法的,请不必感到奇怪,)
typedef void(*Fun)(void);GrandChild gc;int** pVtab = (int**)&gc;cout << "[0] GrandChild::_vptr->" << endl;for (int i=0; (Fun)pVtab[0][i]!=NULL; i++){pFun = (Fun)pVtab[0][i];cout << " ["<<i<<"] ";pFun();}cout << "[1] Parent.iparent = " << (int)pVtab[1] << endl;cout << "[2] Child.ichild = " << (int)pVtab[2] << endl;cout << "[3] GrandChild.igrandchild = " << (int)pVtab[3] << endl;
其运行结果如下所示:(在VC++ 2003和G++ 3.4.4下)
[0] GrandChild::_vptr->
[0] GrandChild::f()
[1] Parent::g()
[2] Parent::h()
[3] GrandChild::g_child()
[4] Child::h1()
[5] GrandChild::h_grandchild()
[1] Parent.iparent = 10
[2] Child.ichild = 100
[3] GrandChild.igrandchild = 1000
使用图片表示如下:
可见以下几个方面:
1)虚函数表在最前面的位置。
2)成员变量根据其继承和声明顺序依次放在后面。
3)在单一的继承中,被overwrite的虚函数在虚函数表中得到了更新。
0 0
- C++对象的内存布局---单继承
- C++ 对象的内存布局(单继承)
- C++对象内存布局:单继承,多继承,虚继承
- (转载)虚继承之单继承的内存布局
- 【C++】c++单继承、多继承、菱形继承内存布局(虚函数表结构)
- 【C++】c++单继承、多继承、菱形继承内存布局(虚函数表结构)
- C++-对象继承中的内存布局
- c++单继承、多继承、菱形继承的内存布局(虚函数表结构)
- 虚继承之单继承的内存布局
- 虚继承之单继承的内存布局
- 虚继承之单继承的内存布局
- 虚继承之单继承的内存布局
- c++虚继承对象的内存布局(修改版)
- C++ 对象的内存布局(多重虚拟继承)
- C++ 对象的内存布局(重复继承)
- C++ 对象的内存布局(重复虚拟继承)
- C++对象的内存布局---多继承
- c++虚继承对象的内存布局
- 临时。。。贵州游
- 《剑指offer》:[53]正则表达式匹配
- 混合易失和非易失主存的日志结构文件系统NOVA[FAST'16]随笔一
- 使用delphi 开发多层应用(三)Delphi常用多层框架介绍
- iOS 拍照及获取相册图片功能
- C++ 对象的内存布局(单继承)
- 使用logstash+elasticsearch+kibana快速搭建日志平台
- 数组常用操作(很实用)
- 【Android学习笔记】RecycleView 绑定了Adapter的item明明设置了match_parent却不起作用
- ehcache整合spring注解方式
- [转]Linux文件权限详解
- [iOS 第三方]画图保存 获取Image(适用签名)
- 手机轮播图手势滑动
- Java反射的作用