昨天面头条,,,最后面试官问道如何用指针访问虚函数表的东西。。。然后瞎写了一通,gg了应该。
昨天的答案
fun = (Fun)((int)(((Base*)0)->f)+4);
fun()
额。。。。昨天的代码是编译也通不过的。。。因为C++是无法获取 任何方法地址的。。。简单的demo测试。。。
#include <iostream>
using namespace std;
class A
{
public:
A(){;}
void f(){;}
virtual void g(){;}
static void h(){;}
};
int main()
{
cout << &A::f << endl;
cout << &A::g << endl;
cout << &A::h << endl;
cout << &A::f << endl;
cout << &A::g << endl;
cout << &A::h << endl;
return 0;
}
输出结果为
$ ./main
1
1
1
1