为了账号安全,请及时绑定邮箱和手机立即绑定

通过虚函数列表指针找到函数实际地址,好像也能证明虚函数列表指针存在

#include<iostream>

using namespace std;


class Shape{

public:

virtual void Areo(){

cout<<"Shape::Areo()"<<endl;

}

};


class Circle:public Shape{

//解开注释,证明覆盖存在

// virtual void Areo()

// {

// cout<<"Circle::Areo"<<endl;

// }

};


int main()

{

Shape s;

cout<<sizeof(s)<<endl;

cout<<&s<<endl;

unsigned int **p=(unsigned int**)&s;

cout<<**p<<endl;

void (*sss)()=(void (*)())(**p);

sss();

cout<<"_______________________"<<endl;

Circle c;

cout<<sizeof(c)<<endl;

cout<<&c<<endl;

unsigned int **q=(unsigned int**)&c;

cout<<**q<<endl;

void (*ccc)()=(void (*)())(**q);

ccc();

return 0;

}


运行结果:

4

0x28fe8c

4317648

Shape::Areo()

_______________________

4

0x28fe88

4317648

Shape::Areo()


--------------------------------


正在回答

1 回答

虚函数列表指针指向的是虚函数表

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之多态篇
  • 参与学习       66223    人
  • 解答问题       334    个

本教程将带领大家体会面向对象三大特性中的多态特性

进入课程

通过虚函数列表指针找到函数实际地址,好像也能证明虚函数列表指针存在

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号