C++利用地址访问虚函数
来源:互联网 发布:2017装修手机淘宝主页 编辑:程序博客网 时间:2024/06/07 06:02
//学习目标:虚函数地址#include "stdafx.h"#include <iostream>using namespace std;//typedef void(*func)(void);函数别名 typedef void(__stdcall *func)(int);//如果构造有参数,则使用这个写法的别名class A{public:int a;virtual void show(int a){cout << "小测" << endl;}virtual void format(int a){cout << "222" << endl;}//虚函数比普通类多4字节,存储虚函数指针。成员函数不占类的空间//地址相差15个字节 3个4字节};class B:public A{public:void show(){cout << "bbbb" << endl;}};int _tmain(int argc, _TCHAR* argv[]){A a;func fp = NULL;A* pr = new A; //a.format();//B b;//b.show();a.show(2);cout << sizeof(A)<<endl;//cout << sizeof(B)<<endl;cout << &a << endl;//cout << &(a.a) << endl;cout << sizeof(a) << endl;//解引用cout << (void*)(*((int*)(&a))) << endl;cout << (int*)(*((int*)(*((int*)(&a))))) << endl;//虚函数列表cout << "虚函数对象: " << (int*)(&a) << endl;cout << "虚函数表首指针地址: " << (int*)*(int*)(&a) << endl;cout << "A虚函数表内第一个函数指针地址: " << (func)*(int*)*(int*)(&a)<< endl;cout << "B虚函数表内第一个函数指针地址: " << (func)*(int*)*(int*)(&a)<< endl;cout << "利用虚函数地址来访问函数:" << endl;//利用地址访问虚函数for (int i = 0; i != 2; ++i){fp = (func)*(((int*)*(int*)(&a)) + i);fp(2);}while(1);return 0;}
阅读全文
1 0
- C++利用地址访问虚函数
- C++中访问虚函数表获取虚函数地址
- C++巧妙利用虚函数访问私有成员
- 函数交换,通过指针访问地址
- 利用FS寄存器得到任意函数地址
- 利用FS寄存器得到任意函数地址
- C函数设置IP地址
- C函数获取IP地址
- 【C语言】数组名&地址&地址中的值的访问
- 利用函数访问私有数据成员
- [C++] 虚指针,虚表,虚函数地址打印
- 利用JNI访问C/VC程序
- x86代码移植到x64下,如何访问虚表和虚函数指针,使用地址调用虚函数
- 打印虚函数地址
- 虚函数地址表
- linux c函数获取系统IP地址
- c 指针与地址 指针与函数
- linux c函数获取系统IP地址
- 框架学习与探究之定时器--Quartz.Net
- 获取系统时间函数
- Sql 存储过程传递参数
- Https系列之一:https的简单介绍及SSL证书的生成
- LeetCode:M-139. Word Break
- C++利用地址访问虚函数
- [实战演练]python3使用requests模块爬取页面内容
- python-模块、包
- 操作系统的内存释放原理
- GKOctree
- String的内存模型,为什么String被设计成不可变的
- 阿里免费分享Java高级开发
- 大数据-Hive服务-用户和用户权限
- ONVIF协议网络摄像机(IPC)客户端程序开发(15):遮挡报警