使用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的接 ...
随机推荐
- Web中的监听器【Listener】
Servlet监听器:Servlet规范中定义的一种特殊类,它用于监听Web应用程序中的ServletContext.HttpSession和ServletRequest等域对象的创建与销毁事件,以及 ...
- cojs 疯狂的粉刷匠 疯狂的斐波那契 题解报告
疯狂的斐波那契 学习了一些奇怪的东西之后出的题目 最外层要模p是显然的,然而内层并不能模p 那么模什么呢,显然是模斐波那契的循环节 那么我们可以一层层的求出每层的斐波那契循环节 之后在从内向外用矩阵乘 ...
- 【Linux高频命令专题(3)】uniq
简述 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] [ -Fields ] [ +Characte ...
- C语言的字符测试函数
C语言的字符测试函数 isalnum, isalpha, isdigit, isxdigit, isblank, isspace, isascii, iscntrl, ispunct, isgraph ...
- 40. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- MyEclipse 2013 开发WebService
1.在Package Explorer窗口右键File新建WebService Project项目,我的名称为:TestWebService 2.WebService Framework选择JAX-W ...
- 转:java两个jre目录和三个lib目录
lib目录下放置着jar包.程序中的import语句找的就是这些文件!例如:import javax.servlet.RequestDispatcher; 问题在于,在cmd模式下编译,系统会提 ...
- NuGet在2015中的使用
NuGet Package Restore https://docs.nuget.org/Consume/Package-Restore 以https://github.com/andburn/hd ...
- android中给TextView或者Button的文字添加阴影效果
1在代码中添加文字阴影 TextView 有一个方法 /** * Gives the text a shadow of the specified radius and color, the ...
- iOS开发:在Xcode中用Pods管理第三方库
之前写了一篇 iOS开发:在Swift中调用oc库 ,今天记录一下如何用Pods的方式来管理第三方库,包括Swift/Object-C的库. 在这之前请先查阅Guides.CocoaPods如何使用的 ...