模拟编译器对虚函数索引项的实现
来源:互联网 发布:俄罗斯女孩中国人知乎 编辑:程序博客网 时间:2024/06/05 15:29
#include <iostream>using namespace std;class A{public:virtual void Func1(){cout << "class A Func1" << endl;}virtual void Func2(){cout << "class A Func2" << endl;}};class B{public:virtual void Func1(){cout << "class B Func1" << endl;}void Func2(){cout << "class B Func2" << endl;}virtual void Func3(){cout << "class B Func3" << endl;}};int* GetFuncAddr(void* p, int offer){//p相当于this指针,off表示虚函数表索引项int* v_ptrAdrs = *((int**)p);return *((int**)v_ptrAdrs + offer);}typedef void (*gFUNC)();void AFunc1Helper(A *p){//直接调用虚函数(*(gFUNC)GetFuncAddr(p, 0))();}//为每一个虚函数,实现一个辅助函数,用于指定虚函数表中的索引项void AFunc2Helper(A *p){ (*(gFUNC)GetFuncAddr(p, 1))();}void BFunc1Helper(B *p){(*(gFUNC)GetFuncAddr(p, 0))();}void BFunc3Helper(B *p){(*(gFUNC)GetFuncAddr(p, 1))();}int _tmain(int argc, _TCHAR* argv[]){A *pa = new A;B *pb = new B;//交换AB虚表指针int v_ptr = *((int*)pa);*((int*)pa) = *((int*)pb);*((int*)pb) = v_ptr;//pa->Func1();AFunc1Helper(pa);//pa->Func2();AFunc2Helper(pa);//pb->Func1();BFunc1Helper(pb);pb->Func2();//pb->Func3();BFunc3Helper(pb);return 0;}
- 模拟编译器对虚函数索引项的实现
- 编译器对虚函数的实现
- 两个c++编译器对c++对象模型和虚函数调用方式的实现
- C++虚函数绑定的编译器实现
- gcc 编译器对 inline 函数的支持
- 编译器优化对函数的影响
- 编译器对构造函数的调用
- 编译器对C函数的名字修饰
- C++编译器对构造函数的影响
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- C++编译器对虚函数和普通成员函数的处理方式
- 在没有实现explicit的编译器模拟explicit
- 编译器眼中的虚函数
- 编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异
- strlen函数的模拟实现
- SQLServer2008镜像实施过程摘要和碰到的问题
- PMOS开关管的选择与电路图
- ASP.NET GridView 实现课程表显示(动态合并单元格)
- HDU 2616 Kill the monster
- webrtc关于锁的编程思想
- 模拟编译器对虚函数索引项的实现
- javascript解析json数据-简单演示
- 每天读一遍,不久你就会变!
- 物联网实验室建设专家
- Comparable和Compara
- 获取硬盘 序列号 信息
- [Boost基础]并发编程——Thread多线程(三)
- 游戏编程合集
- 分享假如你买到缩水U盘了怎么办?认倒霉?肯定不能的!