|
发表于 2016-8-18 21:00:43
|
显示全部楼层
本帖最后由 浅云纸月 于 2016-8-19 22:45 编辑
直接在外部调用基类A的函数也可以实现 ... 不过确实可以实现, 会麻烦一点. 我写了下代码, 你看一下, 不懂的尽管问
- #include "stdafx.h"
- #include <iostream>
- #include <windows.h>
- using namespace std;
- class A
- {
- public:
- virtual void play();
- };
- class B : public A
- {
- public:
- void play();
- };
- void A::play()
- {
-
- cout << "AAA" << endl;
- }
- void B::play()
- {
- cout << "BBB" << endl;
- }
- int main()
- {
- // 获取类 A 的 vftable
- struct tagCLASS
- {
- DWORD *pVMT;
- };
- A a;
- DWORD *pVMT = ((tagCLASS *)(&a))->pVMT;
- // 获取 vftable 中的 play() 函数的内存地址
- static void(__fastcall *pfnA_Play)(void *pthis) = NULL;
- pfnA_Play = (void(__fastcall *)(void *pthis))pVMT[0];
- B b;
- A* aa = &b;
- // 调用 类A 的play()函数
- pfnA_Play(aa);
- return 0;
- }
复制代码 |
|