平台为VS2022,C++14。
很疑惑,明明测试long*指针为8字节,结果获取虚函数地址却还是以4字节作为偏差,不是很懂
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Father
{
?? ?virtual void FatherFun1() { cout << "FatherFun1" << endl; }
?? ?virtual void FatherFun2() { cout << "FatherFun2" << endl; }
?? ?virtual void FatherFun3() { cout << "FatherFun3" << endl; }
};
typedef void (*Fun)(void);
int main()
{
?? ?Father father;
?? ?cout << "类对象地址:" << &father << endl;
?? ?cout << "虚函数表地址: " << (long long*)*(long long*)&father << endl;
?? ?cout << "虚函数FatherFun1地址:" << (long long*)*(long long*)&father << endl;
?? ?cout << "虚函数FatherFun2地址:" << (long long*)*(long long*)&father + 1 << endl;
?? ?cout << "虚函数FatherFun3地址:" << (long long*)*(long long*)&father + 2 << endl;
?? ?cout << sizeof(long long*) << endl;
?? ?cout << "测试地址是否正确" << endl;
?? ?Fun fun = (Fun)*((long long*)*(long long*)&father);
?? ?fun();
?? ?fun = (Fun)*((long long*)*(long long*)(&father) + 1);
?? ?fun();
?? ?fun = (Fun)*((long long*)*(long long*)(&father) + 2);
?? ?fun();
}
|