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

how to get address of member function的更多相关文章

  1. Thinkphp---------Call to a member function free_result() on a non-object

    1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...

  2. :( Call to a member function Table() on a non-object 错误位置

    :( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form

  3. Fatal error: Call to a member function bind_param() on a non-object in

    今天在练习 mysql是出现错误:   Fatal error: Call to a member function bind_param() on a non-object in 解决步骤: 1. ...

  4. ECmall错误:Call to a member function get_users_count() on a non-object

    问题描述: 在后台添加了一个app报错:Call to a member function get_users_count()Fatal error: Call to a member functio ...

  5. magento后台 Fatal error: Call to a member function getId() on a non-object in错误

    后台分类管理出现错误 Fatal error: Call to a member function getId() on a non-object in 在数据库中运行以下sql语句 INSERT I ...

  6. Function语义学之member function

    之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...

  7. Timer.4 - Using a member function as a handler

    In this tutorial we will see how to use a class member function as a callback handler. The program s ...

  8. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  9. About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。

    If only member function clear of WindowMgr is a friend of Screen, there are some points need to note ...

随机推荐

  1. Hive基本命令解析

    1. Hive的分区作用 命令:创建分区 create table t_sz_part(id int, name string) partitioned by (country string) row ...

  2. Spring boot中使用Mongodb

    安装 使用Idea新建Spring boot工程时需要选择Mongodb 或者在工程中加入依赖 Maven: <dependency> <groupId>org.springf ...

  3. node的优缺点及应用场景

    Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎(V8引擎执行Javascript的速度非常快,性能非常好) 可以说node是运行在服务器端V8引擎上的Ja ...

  4. Node 体验 事件驱动、非阻塞式 I/O

    https://github.com/nswbmw/N-blog/blob/master/book/2.1%20require.md 全局对象和浏览器中的window类似 1.console.log( ...

  5. Object 中的wait和Thread中sleep的区别

    摘自 http://www.cnblogs.com/loren-Yang/p/7538482.html 一.区别 1.wait()来自于Object类而sleep来自于Thread类 2.sleep没 ...

  6. Redis是可以安装成windows服务-开机自启

    其实Redis是可以安装成windows服务的,开机自启动,命令如下 redis-server --service-install redis.windows.conf 安装完之后,就可看到Redis ...

  7. ie8 background背景图片不显示问题

    在chrome,FF可以显示,但是在ie8背景图片显示不出来 css改为如下可以正常显示: background: url(../images/goods.png) no-repeat !import ...

  8. event.target解析

    event.target返回最初触发事件的DOM对象. Vue例子: main.js methods:{ fan:function(event){ console.log(event.target); ...

  9. python 的基础 学习 第四天 基础数据类型

    1,数字 int 数字主要是用于计算,使用方法并不是很多,就记住一种就可以. #bit_length() 当十进制用二进制表示时,转化为最少二进制的最少位数v = 11data = v.bit_len ...

  10. UOJ #276「清华集训2016」汽水

    为什么你们常数都这么小啊 UOJ #276 题意:在树上找一条链使得|边权平均值$ -k$|尽量小,$ n<=5e4$ $ Solution:$ 首先二分答案$ ans$,即我们需要找一条链使得 ...