![](/img/trans.png)
[英]Cannot find the virtual function address of derived class in virtual function table
[英]Why virtual function's address of virtual table and virtual function's address on class are different?
下面是我的代码
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <deque>
#include <Windows.h>
class B {
private:
int memB;
public:
B() :memB(0x11111111) {}
virtual void f1() { puts("B::f1"); }
virtual void f2() { puts("B::f2"); }
virtual void f3() { puts("B::f3"); }
void normal() { puts("non virtual"); }
};
class D :public B {
private:
int memD;
public:
D() :memD(0x22222222) {}
void f1() { puts("D::f1"); }
void f2() { puts("D::f2"); }
};
int main() {
B* pB;
B b;
D d;
pB = &b;
pB->f1();
pB->f2();
pB->f3();
pB = &d;
pB->f1();
pB->f2();
pB->f3();
return 0;
}
以上是我的结果图片。
如您所见,B::f1 和 b.__vfptr[0](指向 B::f1)的地址不同。
我认为它们会相同,因为它们指向相同的 function 'B:: f1'。
为什么他们的地址不一样?
我期待着你的好答案。
感谢您的阅读。
在 x86(32 位)调试模式下,MSVC vtable 条目指向 function 蹦床,其中包含实际虚拟功能的 JMP 指令。
如果打开反汇编 window 并输入 vtable 中的地址(例如0xb61276
),您可以很容易地看到它:
B::f1:
00B61276 jmp B::f1 (0B61970h)
可能是为了方便设置软件断点。
在 x86 Release 或 64 位模式中情况并非如此(可能是因为在 64 位模式下 MSVC 调试器总是使用硬件断点)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.