c++虚函数以及虚继承图示
2012-07-16 08:39
621 查看
1. 一般单一继承
只有一个虚表指针,虚表包含所有的虚函数
思考:1. 如果将GrandChild中的h_grandchild前面的virtual去掉,内存中的唯一变化是虚表缺少一项GrandChild::h_grandchild
2. 如果是child: virtual public parent?
output:
addr:0014FA44 sizeof = 28
0:
f1783c = f110c8
GrandChild::g_child()//这个时候要注意,虚函数表里没有f//可以这样理解,虚函数表内的函数和非虚拟继承是一样的,但是基类的位置不一样
Child::h_child()
GrandChild::h_grandchild()
1:
f1784c = fffffffc
fffffffc
10
2:
100
3:
1000
4:
0
5:
f17828 = f11041
GrandChild::f()
Parent::g()
Parent::h()
6:
10
虚基类肯定是放在最后,但是虚函数表中的内容就比较纠结,只放自己被重载的函数。
child和grandchild的指针地址相同,parent的指针地址在最后
2. 一般多继承
3. 虚基类的多继承
请参考:/article/7918004.html
附上本人检测内存分布的函数:
本文中的图片均来自:http://blog.csdn.net/haoel
只有一个虚表指针,虚表包含所有的虚函数
思考:1. 如果将GrandChild中的h_grandchild前面的virtual去掉,内存中的唯一变化是虚表缺少一项GrandChild::h_grandchild
2. 如果是child: virtual public parent?
output:
addr:0014FA44 sizeof = 28
0:
f1783c = f110c8
GrandChild::g_child()//这个时候要注意,虚函数表里没有f//可以这样理解,虚函数表内的函数和非虚拟继承是一样的,但是基类的位置不一样
Child::h_child()
GrandChild::h_grandchild()
1:
f1784c = fffffffc
fffffffc
10
2:
100
3:
1000
4:
0
5:
f17828 = f11041
GrandChild::f()
Parent::g()
Parent::h()
6:
10
虚基类肯定是放在最后,但是虚函数表中的内容就比较纠结,只放自己被重载的函数。
child和grandchild的指针地址相同,parent的指针地址在最后
2. 一般多继承
3. 虚基类的多继承
请参考:/article/7918004.html
附上本人检测内存分布的函数:
typedef void (*Fun)(void); GrandChild b; int *ptr; /*B b;*/ ptr = (int*)&b; cout <<"addr:" << ptr << " sizeof = " << sizeof(GrandChild) <<endl; Fun pFun = NULL; /* Fun pFun = (Fun ) ((int*)ptr[0])[0]; pFun();*/ cout<<"parent:"<<(Parent *)&b<<endl<<"child:"<<(Child *)&b<<endl<<"grandchild:"<<&b<<endl; for(int i=0;i<sizeof(GrandChild)/sizeof(int);i++) { cout<<i<<":"<<endl; if(ptr[i] < 10000) { cout << dec << ptr[i]<<endl; } else { cout << hex << ptr[i] <<" = " << hex << * ((int*)(ptr[i])) <<endl; int *p = (int*)(ptr[i]); if(*p==-4) { cout<<" "<<p[0]<<endl; cout<<" "<<p[1]<<endl; } else { while(*p!=0) { pFun = (Fun )(*p); //cout<<" "<<; //cout.flush(); pFun(); ++p; } } } }
本文中的图片均来自:http://blog.csdn.net/haoel
相关文章推荐
- C++中虚函数的理解,以及简单继承情况下的虚函数的表!
- C++ 06 继承与组合 (has-a is-a) 以及类大小的计算 虚基类对内存模型的影响(不考虑虚函数)
- c++虚函数实现机制以及类继承中的内存分布
- C++ 类的继承,子类以及之类的对象 对父类成员函数的访问权限
- 深入探索c++虚函数继承模型
- c++:继承相关的要点热点,以及菱形继承的底层实现
- C++ 虚函数和虚继承浅析
- C++对象内存布局--③测试多继承中派生类的虚函数在哪一张虚函数表中
- 一个C++继承、虚函数和多态性的实例代码
- C++虚函数和虚继承浅析
- 虚函数,纯虚函数以及虚继承
- C++ 虚函数、纯虚函数、虚继承
- 一道关于C++ 继承/虚函数 笔试题
- C++ 虚函数、纯虚函数、继承、虚表、多态原理相关知识点总结
- c++静态绑定和动态绑定以及虚函数中缺省参数的绑定
- 【整理】C++虚函数及其继承、虚继承类大小
- C++利用纯虚函数和虚函数实现接口继承和实现继承
- 从虚函数的内存构造谈C++继承中函数的覆盖与隐藏
- 【C++面向对象】类的大小以及虚继承
- 继承和多态和虚函数——C++学习笔记二