前言

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

开发环境: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. NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

    解决办法: http://stackoverflow.com/questions/4037125/namespace-err-an-attempt-is-made-to-create-or-chang ...

  2. Linux shell 脚本小记

    if结构 #!/bin/env bash -gt ] then echo "$1 is positive" -lt ] then echo "$1 is negative ...

  3. [hankerrank]Counter game

    https://www.hackerrank.com/contests/w8/challenges/counter-game 关键是二分查找等于或最接近的小于target的数.可以使用和mid+1判断 ...

  4. 50. Pow(x, n)

    题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...

  5. Python数字加千分符

    1.最简单的内置format函数: >>> format(1234567890,',') '1,234,567,890' 2.正则表达式: import re def formatN ...

  6. Regex Tester 安装教程

    下载com.brosinski.eclipse.regex_1.4.0.jar 地址:https://github.com/sbrosinski/RegexTester 下载之后把jar包粘贴到${e ...

  7. [NYIST16]矩形嵌套(DP,最长上升子序列)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=16 像套娃一样把矩形套起来.先给矩形从小到大排序,然后做最长上升子序列就行 /* ━━━━ ...

  8. 3D图形渲染管线

    3D图形渲染管线 什么是渲染(Rendering)    渲染简单的理解可能可以是这样:就是将三维物体或三维场景的描述转化为一幅二维图像,生成的二维图像能很好的反应三维物体或三维场景(如图1):    ...

  9. Java Web编程的主要组件技术——JDBC

    参考书籍:<J2EE开源编程精要15讲> JDBC(Java DataBase Connectivity)是Java Web应用程序开发的最主要API之一.当向数据库查询数据时,Java应 ...

  10. Brew 编译mod错误Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI

    Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI.: Error: ...