C++对象模型----重复继承
2009-08-25 03:06
337 查看
Code:
#include <iostream>
using namespace std;
class A
{
public:
A(int a):x(a){}
virtual void f(){cout << "A::f() called." << endl;}
virtual void g(){cout << "A::g() called." << endl;}
virtual void h(){cout << "A::h() called." << endl;}
private:
int x;
};
class B:public A
{
public:
B(int a,int b):A(a),y(b){}
void f(){cout << "B::f() called." << endl;}
virtual void m(){cout << "B::m() called." << endl;}
virtual void n(){cout << "B::n() called." << endl;}
private:
int y;
};
class C:public A
{
public:
C(int a,int c):A(a),z(c){}
void g(){cout << "C::g() called." << endl;}
virtual void cfun(){cout << "C::cfun() called." << endl;}
private:
int z;
};
class D:public B,public C
{
public:
D(int a,int b,int c,int d):B(a,b),C(a,c),d(d){}
void h(){cout << "D::h() called." << endl;}
void m(){cout << "D::m() called." << endl;}
virtual void t(){cout << "D::t() called." << endl;}
private:
int d;
};
int main()
{
D tp(2,3,4,5);
typedef void (*PFUN)(void);
PFUN pfun;
pfun = (PFUN)(*((int*)(*(int*)(&tp))));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+1));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+2));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+3));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+4));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+5));
pfun();
cout << *((int*)(&tp) + 1) << endl;
cout << *((int*)(&tp) + 2) << endl;
pfun = (PFUN)(*(int*)(*((int*)(&tp) + 3)));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+1));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+2));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+3));
pfun();
cout << *((int*)(&tp) + 4) << endl;
cout << *((int*)(&tp) + 5) << endl;
cout << *((int*)(&tp) + 6) << endl;
return 0;
}
对像模型:
![](file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/)966MUO%7B7H52GRXZ@B~_G6E.jpg)
------------------------------------------------------------------------------------
从输出结果上我们可以看到A::x在对象D中有两份。一份来自于B,一份来自于C。这不仅浪费的存储空间,而且容易造成歧义。故此引入了虚拟继承。
#include <iostream>
using namespace std;
class A
{
public:
A(int a):x(a){}
virtual void f(){cout << "A::f() called." << endl;}
virtual void g(){cout << "A::g() called." << endl;}
virtual void h(){cout << "A::h() called." << endl;}
private:
int x;
};
class B:public A
{
public:
B(int a,int b):A(a),y(b){}
void f(){cout << "B::f() called." << endl;}
virtual void m(){cout << "B::m() called." << endl;}
virtual void n(){cout << "B::n() called." << endl;}
private:
int y;
};
class C:public A
{
public:
C(int a,int c):A(a),z(c){}
void g(){cout << "C::g() called." << endl;}
virtual void cfun(){cout << "C::cfun() called." << endl;}
private:
int z;
};
class D:public B,public C
{
public:
D(int a,int b,int c,int d):B(a,b),C(a,c),d(d){}
void h(){cout << "D::h() called." << endl;}
void m(){cout << "D::m() called." << endl;}
virtual void t(){cout << "D::t() called." << endl;}
private:
int d;
};
int main()
{
D tp(2,3,4,5);
typedef void (*PFUN)(void);
PFUN pfun;
pfun = (PFUN)(*((int*)(*(int*)(&tp))));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+1));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+2));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+3));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+4));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&tp))+5));
pfun();
cout << *((int*)(&tp) + 1) << endl;
cout << *((int*)(&tp) + 2) << endl;
pfun = (PFUN)(*(int*)(*((int*)(&tp) + 3)));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+1));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+2));
pfun();
pfun = (PFUN)(*((int*)(*((int*)(&tp) + 3))+3));
pfun();
cout << *((int*)(&tp) + 4) << endl;
cout << *((int*)(&tp) + 5) << endl;
cout << *((int*)(&tp) + 6) << endl;
return 0;
}
对像模型:
![](file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/)966MUO%7B7H52GRXZ@B~_G6E.jpg)
------------------------------------------------------------------------------------
从输出结果上我们可以看到A::x在对象D中有两份。一份来自于B,一份来自于C。这不仅浪费的存储空间,而且容易造成歧义。故此引入了虚拟继承。
相关文章推荐
- C++继承详解之三——菱形继承+虚继承内存对象模型详解vbptr(1)
- C++虚拟多重继承对象模型讨论
- C++之虚继承和虚函数对C++对象内存模型造成的影响(类/对象的大小)
- C++虚函数、虚继承、对象内存模型
- C++ 虚继承和虚函数同时存在的对象模型
- C++对象模型——"继承"与Data Member(第三章)
- 虚继承内存布局@c++对象模型
- c++中各类继承下的对象模型
- [C++对象模型][7]单继承与虚函数表
- C++继承中的对象模型
- 继承体系下C++对象模型.md
- [原创]C++虚继承内存对象模型探讨
- c++对象模型之菱形继承
- C++ 继承中的对象模型
- c++对象模型学习心得(初步继承模型)
- C++中多态与对象模型及菱形继承
- C++多重继承和虚拟继承对象模型、效率分析
- C++-对象继承内存模型配图
- C++对象内存模型(高级)—多重继承与虚函数表(转)
- C++对象模型 多重继承与虚函数表