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 ...
随机推荐
- 01-maya基础
maya基础 1,ctrl shift m 切换面板工具栏. 2,空格键+ 右键 :快速的切换视图. 3,在一视图上单击空格键,可放大显示. 4, 工程的创建 1,创建工程:文件--项目窗口,建完后, ...
- hadoop的基本概念 伪分布式hadoop集群的安装 hdfs mapreduce的演示
hadoop 解决问题: 海量数据存储(HDFS) 海量数据的分析(MapReduce) 资源管理调度(YARN)
- Baidu 人脸识别FireFly 与PC连接调试
1.USB线插到离屏幕较远的双层USB口上方.2.安装驱动,OK.,然后就可以直接拷贝安装包或者连接调试了. 其它几个口都不行.
- Javascript async异步操作库简介
异步操作知识 在js世界中, 异步操作非常流行, nodejs就是特点基于异步非阻塞. js语言支持的异步语法包括, Promise async await generator yield. 这些语 ...
- Python3快速入门
——<趣学Python-教孩子学编程>学习笔记 1.注释 (1)单行注释以 # 开头注释 # 这是一个注释 print("Hello, World!") (2)多行 ...
- 利用ssh操控远程服务器
这里的”远程”操控的方法实际上也不是真正的远程.,這此操作方法主要是在一个局域网内远程操控电脑 (在一个路由器下).可以把它做成在互联网中的远程操控, 不过技术难度上加了一个等级, 如果你想是想人在公 ...
- ACM2作业
文件读写知识点: 写入文件:freopen("文件名", "r", stdin); 写出文件:freopen("文件名", "w& ...
- sockaddr_in 与 in_addr的区别
struct sockaddr_in {short int sin_family; /* 地址族 */unsigned short int sin_port; /* 端口号 */struct in_a ...
- 【页面加载】【九九乘法表】【document.write的功能_】【<script>直接显示数组】【声明新变量】
1.页面加载时向body加载文本.弹出框 <body> <script> document.write("<h1>Ja ...
- DeepLearning.ai-Week3-Autonomous driving-Car detection
1 - Import Packages import argparse import os import matplotlib.pyplot as plt from matplotlib.pyplot ...