博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++获取类成员函数地址
阅读量:6857 次
发布时间:2019-06-26

本文共 588 字,大约阅读时间需要 1 分钟。

C语言中可以用函数地址直接调用函数:
实例代码:
void print ()
{
   printf ("function print");
}
 
typdef void (*fun)();
fun f = print;
f();
 
C++中类非静态成员函数必须通过实例去调用,C++中类成员函数调用:
class test
{
public:
void print ()
{
   printf ("function print");
}
};
 
我们同样可以通过定义函数指针来调用如下:
typedef void (test::*fun)();
fun f = &test::print;
test t;
(t.*f)();
 
如果我们想取函数地址怎么办,直接强转会报错
unsigned int a = reinterpret_cast<unsigned int>(f);//编译错误
这里给出了答案.
char buf [32] = {0};
sprintf(buf , "%u", f);
int a = atoi( buf);
 
其实我们知道类成员函数只是函数第一个参数是this指针,但是this指针不是通过普通函数参数压栈方式,而是放进ecx中.

转载于:https://www.cnblogs.com/ourroad/archive/2013/06/08/3127609.html

你可能感兴趣的文章
谷歌用户体验设计准则
查看>>
LaTeX中的数学公式
查看>>
计算二重定积分
查看>>
asp.net Web项目中c#读取域用户名的方法
查看>>
linux下安装apache及php
查看>>
重绘TabControl
查看>>
Python Decorator
查看>>
delphi debug release区别是什么?
查看>>
理解Docker容器的进程管理
查看>>
微信小程序独家秘笈之左滑删除
查看>>
Using Oracle's Parallel Execution Features
查看>>
[轉]发布时出现PHP has encountered a Stack overflow 解决办法
查看>>
substitute Simple JavaScript Template :
查看>>
9、如何清空流及缓存
查看>>
gvim汉化及配置
查看>>
ubuntu下JMF RTP不支持单播接收
查看>>
fastboot烧写命令
查看>>
深度测试与alpha混合(1)
查看>>
15幅非常有创意的影子摄影作品欣赏
查看>>
lightbox源码解析
查看>>