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. [Android] Android 卡片式控件CardView的优雅使用

    [Android] Android 卡片式控件CardView的优雅使用 CardView是在安卓5.0提出的卡片式控件 其具体用法如下: 1.在app/build.gradle 文件中添加 comp ...

  2. 测试四则运算2:Right-BICEP

    n6个值得测试的具体部位,他们能够提高你的测试技巧 nRight-结果是否正确? nB-是否所有的边界条件都是正确的? nI-能查一下反向关联吗 nC-能用其他手段交叉检查一下结果吗? nE-你是否可 ...

  3. luogu P2387 [NOI2014]魔法森林

    传送门 这题似乎不好直接做,可以考虑按照\(a_i\)升序排序,然后依次加边更新答案 具体实现方法是用lct维护当前的树,这里需要维护链上最大的\(b_i\).每次加一条边,如果加完以后没有环直接加, ...

  4. luogu P4774 [NOI2018]屠龙勇士

    传送门 这题真的是送温暖啊qwq,而且最重要的是yyb巨佬在Day2前几天正好学了crt,还写了博客 然而我都没仔细看,结果我就同步赛打铁了QAQ 我们可以先根据题意,使用set维护,求出每次的攻击力 ...

  5. spring cloud(学习笔记)微服务启动错误(1)

    今天下午在启动spring cloud微服务的时候,报了这个错误: Error starting ApplicationContext. To display the auto-configurati ...

  6. mongodb系列~mongodb定时删除数据

    一 简介:本文介绍创建自动删除数据的TTL索引 二 目的 定时删除数据三 创建方法   db.collection.createIndex(keys, options)   options:   ex ...

  7. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  8. docker remote api 的安全隐患

    开启docker的api,首先要知道docker的守护进程daemon,在下认为daemon作为Client和service连接的一个桥梁,负责代替将client的请求传递给service端. 默认情 ...

  9. linux 日志管理

  10. cxf方式实现webservice接口笔记

    1.配置cxf的jar包依赖: 因maven中央仓库会自带cxf的相关依赖jar,故去maven官网搜索并获取pom配置到本地pom.xml文件即可,maven中央仓库搜索引擎地址:http://se ...