使用std::function 把类成员函数指针转换为普通函数指针
前言
这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可。
开发环境:WIN7 32位 + VS2010
发现在VS2005中使用std::funtion报错:
错误 1 error C2039: “function”: 不是“std”的成员 e:\vsprojectsforvms\designpattern\observer2\observer2.cpp 123
于是改为VS2010来写。
#include "stdafx.h" //std::function需要此头文件
#include <functional> #include <vector>
#include <iostream> //std::find需要此头文件
#include <algorithm>
using namespace std; /********************************************
First类和Second相当于两个观察者, 他们两个没有继承结构
First类和Second类的更新函数原型相同,函数名不必相同
********************************************/ class First
{
public:
int Add1(int a, int b) //更新函数
{
return a + b;
}
}; class Second
{
public:
int Add2(int a, int b) //更新函数,
{
return a + b;
} }; class CTest
{ public: typedef std::tr1::function<int(int, int)> PAdd; /*Attach函数来增加观察者的更新函数
由于std::function没有重载operator ==, 因此不能用std::find函数, 也不能在Remove中使用*ter == pAdd这样的比较。
*/ void Attach(PAdd pAdd)
{ // if (std::find(m_vecPtr.begin(), m_vecPtr.end(), pAdd) == m_vecPtr.end())
{
m_vecPtr.push_back(pAdd);
} } void Remove(PAdd pAdd)
{
//试图删除观察者的更新函数,报错
/*for (vector<PAdd>::iterator iter = m_vecPtr.begin(); iter != m_vecPtr.end(); ++ iter)
{
if (*iter == pAdd)
{
m_vecPtr.erase(iter);
return;
}
}*/
} void Notify()
{
for (vector<PAdd>::const_iterator iter = m_vecPtr.begin();
iter != m_vecPtr.end(); ++ iter)
{
int sum = (*iter)(, );
cout<<"result is "<<sum<<endl;
}
} protected:
vector<PAdd> m_vecPtr;
}; int _tmain(int argc, _TCHAR* argv[])
{
First objFirst;
Second objSecond; CSubject obj; //需要说明的是:std::bind函数的第三第四个参数表示参数的占位符,即对应的Add函数有N个参数,
//这里就有std::placeholders::_1,std::placeholders::_2, ... std::placeholders::_N
CSubject::PAdd pAdd1 = std::bind(&First::Add1, &objFirst, std::placeholders::_1, std::placeholders::_2);
CSubject::PAdd pAdd2 = std::bind(&Second::Add2, &objSecond, std::placeholders::_1, std::placeholders::_2);
obj.Attach(pAdd1); obj.Attach(pAdd2); obj.Notify(); return ;
}
执行结果:
至于怎么样让程序在调用(*iter)时传入不同的参数,可以修改一下Attach函数,改为类似
void Attach(PAdd, int, int)这样的形式,让函数指针和两个参数一一对应,再调用AddX函数时去查找该函数对应的两个参数,然后传给(*iter)(参数1, 参数1)。
使用std::function 把类成员函数指针转换为普通函数指针的更多相关文章
- typedef 函数指针 数组 std::function
1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA, *PDATA; //D ...
- C++11中的std::function
看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...
- 【转】C++11中的std::function
原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...
- std::function 的使用说明
转自: https://www.cnblogs.com/heartchord/p/5017071.html //////////////////// std::function 参考资料 • cp ...
- std::function
参考资料 • cplusplus.com:http://www.cplusplus.com/reference/functional/function/ • cppreference.com:http ...
- 第11课 std::bind和std::function(2)_std::bind绑定器
1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...
- std::function 使用_
#include <iostream> #include <functional> //函数指针写法 typedef int(*FuncPoint)(const int& ...
- C++11中std::function的使用
class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...
- 剖析std::function接口与实现
目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...
随机推荐
- 查看服务器的TCP/IP(http)连接情况
6.那具体到,怎样监听一个具体程序的TCP/IP连接情况呢???!! 就要用:netstat -n|grep 80 命令来查看具体端口对应程序的TCP/IP连接情况了 netstat -n|grep ...
- windows下顽固软件卸载不了的解决方法
下面以autocad2012举例: cad2012卸载显示 “无法获得同类产品” 而且也安装不上. 解决方法:1,开始>运行>输入"regedit",找到下面的注册表路 ...
- TCL语言笔记:TCL中的控制结构命令
一.引言 控制结构允许程序根据不同的状态.条件和参数来选择不同的处理和执行路径,从而使代码具有更强的灵活性.健壮性和可读性. Tcl 提供了 if.if/else.if/elseif.foreach. ...
- PowerDesigner中转换物理模型时的命名转换
原文:PowerDesigner中转换物理模型时的命名转换 最近在使用PowerDesigner建模数据库,在使用中积累了一些遇到的问题和解决办法,记录下来,希望对遇到同样问题的朋友有所帮助. 早 期 ...
- Linux下禁用、启用SeLinux
一些Linux默认都是启用SeLinux的,在安装操作系统的时候我们可以选择开启或者关闭SeLinux,但是在安装完系统之后又如何开启与关闭呢? 在/etc/sysconf下有一个SeLinux文件, ...
- poi操作oracle数据库导出excel文件2
package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- python判断文件目录是否存在
import os os.path.isfile('test.txt') # 如果不存在就返回False os.path.exists(directory) # 如果目录不存在就返回False o ...
- Hibernate映射集合属性
Hibernate要求持久化集合属性字段必须声明为接口,实际的接口可以是java.util.Set,java.util.Collection,java.util.List,java.util.Map, ...
- LA 4119 (差分数列 多项式) Always an integer
题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...
- CodeForces Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...