C++对象模型——单继承
2009-08-24 14:26
197 查看
Code:
#include <iostream>
using namespace std;
class point
{
public:
point(float a):x(a){}
virtual void f(){cout << "point::f() called" << endl;}
virtual void g(){cout << "point::g() called" << endl;}
void h(){cout << "point::h() called" << endl;}
public:
float x;
};
class point2D:public point
{
public:
point2D(float a,float b):point(a),y(b){}
void g(){ cout << "point2D::g() called" << endl;}
virtual void z(){cout << "point2D::z() called" << endl;}
private:
float y;
};
int main()
{
point2D t(5,6);
typedef void (*PFUN)();
PFUN pfun;
pfun = (PFUN)(*(int*)(*(int*)(&t)));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&t))+1));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&t))+2));
pfun();
cout << *(float*)((float*)(&t)+1)<< endl;
cout << *(float*)((float*)(&t)+2)<< endl;
return 0;
}
point的对象模型:
4B vptr -----------------> point::f();
4B float x; point::g();
point2D的对象模型:
4B vptr----------------->point::f();
4B point::x; point2D::g():
4B point2D::y; point2D::z();
#include <iostream>
using namespace std;
class point
{
public:
point(float a):x(a){}
virtual void f(){cout << "point::f() called" << endl;}
virtual void g(){cout << "point::g() called" << endl;}
void h(){cout << "point::h() called" << endl;}
public:
float x;
};
class point2D:public point
{
public:
point2D(float a,float b):point(a),y(b){}
void g(){ cout << "point2D::g() called" << endl;}
virtual void z(){cout << "point2D::z() called" << endl;}
private:
float y;
};
int main()
{
point2D t(5,6);
typedef void (*PFUN)();
PFUN pfun;
pfun = (PFUN)(*(int*)(*(int*)(&t)));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&t))+1));
pfun();
pfun = (PFUN)(*((int*)(*(int*)(&t))+2));
pfun();
cout << *(float*)((float*)(&t)+1)<< endl;
cout << *(float*)((float*)(&t)+2)<< endl;
return 0;
}
point的对象模型:
4B vptr -----------------> point::f();
4B float x; point::g();
point2D的对象模型:
4B vptr----------------->point::f();
4B point::x; point2D::g():
4B point2D::y; point2D::z();
相关文章推荐
- Microsoft Visual C++虚拟多继承 对象模型初步分析
- C++对象模型-----多继承
- c++对象模型之菱形继承
- c++中各类继承下的对象模型
- C++继承中的对象模型
- [置顶] 从零开始学C++之虚继承和虚函数对C++对象内存模型造成的影响(类/对象的大小)
- 虚继承和虚函数对c++对象存储模型的影响(类/对象的大小)
- [C++对象模型][8]多重继承与虚函数表
- 【C++】菱形虚拟继承对象模型分析
- C++虚拟继承中的对象模型
- C++虚函数、虚继承、对象内存模型
- C++虚拟多重继承对象模型讨论
- 深度探索C++对象模型--继承与Data Member
- C++继承详解之三——菱形继承+虚继承内存对象模型详解vbptr(1)
- 继承体系下C++对象模型.md
- [C++对象模型][9]虚继承与虚函数表
- 虚继承内存布局@c++对象模型
- C++对象模型——"继承"与Data Member(第三章)
- 浅谈c++继承与对象模型
- C++-对象继承内存模型配图