通过虚函数列表指针找到函数实际地址,好像也能证明虚函数列表指针存在
#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()
--------------------------------