您的位置:首页 > 编程语言 > C语言/C++

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: