前言

这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可。

开发环境: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 把类成员函数指针转换为普通函数指针的更多相关文章

  1. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  2. C++11中的std::function

    看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...

  3. 【转】C++11中的std::function

    原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...

  4. std::function 的使用说明

    转自: https://www.cnblogs.com/heartchord/p/5017071.html //////////////////// std::function   参考资料 • cp ...

  5. std::function

    参考资料 • cplusplus.com:http://www.cplusplus.com/reference/functional/function/ • cppreference.com:http ...

  6. 第11课 std::bind和std::function(2)_std::bind绑定器

    1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...

  7. std::function 使用_

    #include <iostream> #include <functional> //函数指针写法 typedef int(*FuncPoint)(const int& ...

  8. C++11中std::function的使用

    class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...

  9. 剖析std::function接口与实现

    目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...

随机推荐

  1. lintcode :搜索二维矩阵

    题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...

  2. hdu 3863 No Gambling

    #include<stdio.h> int main() { int n; ) { printf("I bet on Oregon Maple~\n"); } ; } ...

  3. HTML5入门十一---Canvas画布实现画图(二)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 关于delphi Assigned

    1. 根据 Delphi 指令参考手册中 说明: Assigned 函式在参数不为 nil 时传回 True, 表示指针已经指到某个内存地址,这个内存地址可能是一个对象地首地址,也可能在函数或过程中, ...

  5. .md文件 Markdown 语法说明

    Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 强调 ...

  6. iOS LLDB调试器

    随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...

  7. 控制CPU占用率曲线

    编程之美的第一个问题,我的机器是双核的,用文中的代码,得到的曲线波动比较大额,受其他进程的影响比较大.文中提到10ms接近windows的调度时间片,如果选得太小,会造成线程被频繁唤醒和挂起,造成内核 ...

  8. Eclipse - FindBugs Plugin 的安装和使用

    Eclipse -  FindBugs Plugin 的安装和使用 FindBugs is a static analysis tool that examines the classes in se ...

  9. java-基础练习题

    [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1 ...

  10. tomcat web.xml 配置

    1<web-app> 2<error-page> 3<error-code>404</error-code> 4<location>/Not ...