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. centos7.0 下安装git(http方式)
  2. Java反编译代码对齐
  3. Linux查找
  4. 字节流InputStream/OutputStream
  5. Win7配置Nginx+PHP7
  6. storm的并发和消息保障性
  7. Unity-Animator深入系列---FAQ
  8. linux服务之snmp
  9. SDUST 软件工程2016-作业4-A 百钱买鸡问题
  10. validate中remote的用法
  11. git 401 错误
  12. Flex中如何通过showAllDataTips属性使鼠标移动到图表时显示所有的数据Tips的例子
  13. PHP版本的区别
  14. Private和Protected方法
  15. C#中的out参数/ref参数/params可变参数
  16. php密码对称encrypt加密
  17. 如何创建并初始化程序集里List类型的反射
  18. Multithreading C++ Out of Core Sotring for Massive Data|多线程C++的大规模数据外部排序
  19. [翻译]SQL Server等待事件&mdash;THREADPOOL
  20. 剑指Offer 53. 表示数值的字符串 (字符串)

热门文章

  1. JVM简析
  2. 自己制作redis 和mongo 镜像
  3. 038、Docker 的两类存储资源(2019-02-27 周三)
  4. tomcat自动重新加载应用
  5. Carbon - 在线生成精美的代码片段图片(含插件)
  6. Immunity Debugger学习 二
  7. ubuntu安装matplotlib一些坑
  8. file图片上传之前先预览
  9. 4-23 模块 hashlib ,configparser,loging,collections
  10. day 4 - 2 数据类型练习