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);
}

最新文章

  1. [nRF51822] 11、基础实验代码解析大全 &#183; 实验16 - 内部FLASH读写
  2. linux安装open block chain
  3. 整数划分问题-解法汇总(暂有DP-递归)
  4. 【hadoop2.6.0】利用JAVA API 实现数据上传
  5. PHP面向对象学习五 类中接口的应用
  6. X/Y型文案
  7. UESTC 882 冬马党 --状压DP
  8. AwSnap:让全版本(Windows、iOS、Android)Chrome浏览器崩溃的有趣漏洞
  9. SQL SERVER中架构的理解
  10. 191. Number of 1 Bits
  11. python 读取机器信息
  12. HW5.23
  13. 【译】addEventListener 第二个参数
  14. DenyHosts安装及配置
  15. vs10创建sqlclr部署失败
  16. 持续集成的一些讨论(CI)
  17. Python核心编程笔记 第二章
  18. m2eclipse(maven插件)报错解决
  19. you must restart adb and eclipse的相关解决办法
  20. 发现vi出现此错误~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so: undefined symbol: clang_getCompletionFixIt

热门文章

  1. Git与GitHub学习笔记(一)如何删除github里面的文件夹?
  2. Spark整合HBase,Hive
  3. oldboy s21day02
  4. Unicode与UTF8
  5. ASP.NET MVC之视图传参到控制器的几种形式
  6. IT界的一些朗朗上口的名言
  7. @Component注解的解析
  8. Kafka架构简介
  9. vue请求java服务端并返回数据
  10. Python 基础算法