how to get address of member function
see:http://www.cplusplus.com/forum/general/136410/ & http://stackoverflow.com/questions/8121320/get-memory-address-of-member-function
static member function and dynamic member functions are treated differently.
we can just use std::function() to realize it
#include<iostream>
using namespace std;
class A
{
public:
void ppp() { cout << "function ppp() was called" << endl; }
static void bbb(void* obj){((A*)obj)->ppp(); }
}; // save addresses of object and member functions
// and then use template to help get the right class type
class Caller
{
public:
class icaller{ public:virtual void run() = ; };
template<class T>
class caller :public icaller {
public:
caller(T * obj, void(T::*call)()) :object(obj), func(call){}
virtual void run(){ (object->*func)(); }
T * object;
void(T::*func)();
};
template<class T>
Caller(T*object, void (T::*clk)()){ p = new caller<T>(object, clk); }
icaller * p;
void run() { p->run(); }
}; // function 2: save the addresses of object and static member functions
// then call dynamic functions through static member functions
class Callb
{
public: void setcall(void(*cb)(void *), void *obj){ callbk = cb; object = obj; }
void run(){ (*callbk)(object); }
void(*callbk)(void *);
void *object;
};
int main()
{
A a;
void(A::*func)() = &A::ppp;
void(*funcd)(void *) = &A::bbb;
Caller funca(&a, &A::ppp);
Callb funcb;
funcb.setcall(&A::bbb, &a);
funcb.run();
funca.run();
(a.*func)();
funcd(&a);
}
最新文章
- [nRF51822] 11、基础实验代码解析大全 &#183; 实验16 - 内部FLASH读写
- linux安装open block chain
- 整数划分问题-解法汇总(暂有DP-递归)
- 【hadoop2.6.0】利用JAVA API 实现数据上传
- PHP面向对象学习五 类中接口的应用
- X/Y型文案
- UESTC 882 冬马党 --状压DP
- AwSnap:让全版本(Windows、iOS、Android)Chrome浏览器崩溃的有趣漏洞
- SQL SERVER中架构的理解
- 191. Number of 1 Bits
- python 读取机器信息
- HW5.23
- 【译】addEventListener 第二个参数
- DenyHosts安装及配置
- vs10创建sqlclr部署失败
- 持续集成的一些讨论(CI)
- Python核心编程笔记 第二章
- m2eclipse(maven插件)报错解决
- you must restart adb and eclipse的相关解决办法
- 发现vi出现此错误~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so: undefined symbol: clang_getCompletionFixIt