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);
}
how to get address of member function的更多相关文章
- Thinkphp---------Call to a member function free_result() on a non-object
1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form
- 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. ...
- 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 ...
- 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 ...
- Function语义学之member function
之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...
- 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 ...
- C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化
模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...
- 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 ...
随机推荐
- python 精华梳理(已理解并手写)--全是干货--已结
基础部分 map,reduce,filter,sort,推导式,匿名函数lambda , 协程,异步io,上下文管理 自定义字符串转数字方法一不使用reduce import re def str2i ...
- 在 CentOS6 上安装 Zabbix3.0 Agent 并开启客户端自动注册
#!/bin/bash # # .配置yum源 # cat /etc/redhat-release |grep -i centos |grep '6.[[:digit:]]' &>/de ...
- 在py文件中设置文件头
在写python文件的时候有时需要记录作者.创建时间等时间,因此可以给python文件设置文件头,这里以PyCharm为例介绍设置步骤: 1. 打开PyCharm,依次点击Setting-----Ed ...
- PHP操作实现一个多功能购物网站
PHP操作实现一个多功能购物网站 一.需要实现的页面: Index.aspx:浏览商品页面,显示商品列表,用户可以点击“加入购物车“. ViewCart.aspx:查看购物车页面,显示已购买的商品信息 ...
- Docker 操作记录
Docker docker info 查看信息 docker 更改镜像目录 方式:1.配置文件修改 # cat /etc/sysconfig/docker |grep -v ^# |grep -v ^ ...
- Coursera, Deep Learning 4, Convolutional Neural Networks - week2
Case Study (Note: 红色表示不重要) LeNet-5 起初用来识别手写数字灰度图片 AlexNet 输入的是227x227x3 的图片,输出1000 种类的结果 VGG VGG比Ale ...
- jinja模板语法
模板 要了解jinja2,那么需要先理解模板的概念.模板在Python的web开发中广泛使用,它能够有效的将业务逻辑和页面逻辑分开,使代码可读性增强.并且更加容易理解和维护. 模板简单来说就是一个其中 ...
- 缓存设计(cache-design)
分布式缓存设计 目前常见的缓存方案都是分层缓存,通常可以分为以下几层: 1.1NG本地缓存,命中的话直接返回 1.2 NG没有命中时则需要查询分布式缓存,如redis 1.3 如果分布式缓存没有命中则 ...
- Java基础_0302:类和对象
定义类 class Book { // 定义一个新的类 String title; // 书的名字 double price; // 书的价格 /** * 输出对象完整信息 */ public voi ...
- Javascript入门(三)函数
Javascript函数 一.函数定义与执行 <script type="text/javascript"> //define function fun1(){ ale ...