Function object is very userful to use member function or non-member function as callback mechanism,
Same as event or delegate in C#.

For pointer to object

 template <class Return, class Type, class Argument>
class MemFunctionObject : public binary_function <Type*,Argument,Return>
{
Return (Type::*pMemFunction)(Argument);
public:
explicit MemFunctionObject( Return (Type::*pMF)(Argument) ) : pMemFunction (pMF)
{
}
Return operator() (Type* pObject, A x) const
{
return (pObject->*pMemFunction)(x);
}
};

For reference to object

 template <class Result, class Type, class Argument>
class MemFunctionObjectRef: public binary_function <Type,Argument,S>
{
private:
Result (Type::*pMemMethod)(Argument);
public:
explicit MemFunctionObjectRef ( Result (Type::*p)(Argument) ) : pMemMethod (p) {}
Result operator() (Type& refObject, Argument x) const
{
return (refObject.*pMemMethod)(x);
}
};

 binary_function  define type's alias

 template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};

Now, wo can define a function to generate a function object

 template <class Result, class Type, class Argument>
MemFunctionObject<Result,Type,Argument> MemFunction(Result (Type::*f)(Argument))
{
return MemFunctionObject<Result,Type,Argument>(f);
}

How to use:

 // mem_fun example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std; int main () {
vector <string*> numbers; // populate vector of pointers:
numbers.push_back ( new string ("one") );
numbers.push_back ( new string ("two") );
numbers.push_back ( new string ("three") );
numbers.push_back ( new string ("four") );
numbers.push_back ( new string ("five") ); vector <int> lengths ( numbers.size() ); transform (numbers.begin(), numbers.end(), lengths.begin(),MemFunction(&string::append,_1,"_test!").length()); for (int i=; i<; i++) {
cout << *numbers[i] << " has " << lengths[i] << " letters.\n";
}
return ;
}



Function Object in C++的更多相关文章

  1. 【STL】-function object

    // Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...

  2. 认识js函数对象(Function Object)

    认识函数对象(Function Object) 可以用function关键字定义一个函数,对于每个函数可以为其指定一个函数名,通过函 数名来进行调用.这些都是代码给用户的印象,而在JavaScript ...

  3. Javascript中Function,Object,Prototypes,__proto__等概念详解

    http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...

  4. flask_route错误:AttributeError: 'function' object has no attribute 'route'

    问题: 路由完全正确,当只有一个名为home的函数处理这个路由时候,下一个路由处理函数,总是提示没有这个rotue属性 Traceback (most recent call last): File ...

  5. 函数对象与仿函数(function object and functor)

    part 1. 仿函数在STL组件中的关系 如下图: # 仿函数配合算法完成不同的策略变化. # 适配器套接仿函数. part 2. 仿函数介绍 传递给算法的“函数型实参”不一定得是函数,可以是行为类 ...

  6. C++ 谓词(predicate) 与 仿函数 ( functor (function object))

    谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a f ...

  7. tensorboardX使用中 AttributeError: 'function' object has no attribute 'graph'

    最近在使用tensorboardX可视化网络结构,因为tensorboardX并非pytorch原生的可视化工具,所以版本之间并不兼容 在使用的过程中会遇到,AttributeError: 'func ...

  8. js常用数据类型(Number,String,undefined,boolean) 引用类型( function,object,null ),其他数据类型( 数组Array,时间Date,正则RegExp ),数组与对象的使用

    js常用数据类型 数字类型 | 字符串类型 | 未定义类型 | 布尔类型 typeof()函数查看变量类型 数字类型  Number var a1 = 10; var a2 = 3.66; conso ...

  9. JavaScript:Function/Object/prototype/__proto__

    console.log(Object.__proto__===Function.prototype); //true console.log(Object.prototype.__proto__); ...

随机推荐

  1. date命令的基本用法

    date设置时间 设置时间:-s参数 date -s "20190426 15:22:33" date查看时间差  (-d参数多用于脚本) 查看时间差:-d参数date -d &q ...

  2. SpringBoot 整合 中国移动 MAS HTTP1.0 实现短信发送服务

    因为客户需要,本身使用的 阿里云的短信服务改为了中国移动MAS HTTP 1.0  短信通知,因为看到网络上关于此类的博客知识很少,再趟完坑后特地写下这篇博客,提醒后来人. 特别感谢 中国移动MAS ...

  3. Prufer序列与树的计数(坑)

    \(prufer\)序列: 无根树转\(prufer\)序列: 不断找编号最小的叶子节点,删掉并在序列中加入他相连的节点. \(prufer\)转无根树: 找到在目前\(prufer\)序列中未出现且 ...

  4. Python爬虫学习:Python内置的爬虫模块urllib库

    urllib库 urllib库是Python中一个最基本的网络请求的库.它可以模拟浏览器的行为发送请求(都是这样),从而获取返回的数据 urllib.request 在Python3的urllib库当 ...

  5. CentOS 开启安装EPEL YUM源

    我们用yum安装软件时,经常发现我们的yum源里面没有该软件,需要自己去wget,然后configure,make,make install,太折腾了. 其实,CentOS 还有一个源叫做 EPEL ...

  6. Web App、Hybrid App、 Native App

    1.特点: 1. 偏交互的Native,偏浏览的Web:交互指复杂操作,输入/选择什么的2. 已稳定的Native,试错中的Web:H5页面用来做低成本验证很好3. 访问硬件Native,信息展示We ...

  7. 20.Class的继承

    1.简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多 class Point { } class ColorPoint extends ...

  8. maven项目debug调试不能够进入源码问题解决

    Maven项目在debug调试模式的时候,进入调试模式,但是没有进入源码界面. 上述问题的解决方法如下: 第一步: 第二步: 第三步: 第四步: 第五步: 到这里就解决了:

  9. 关于clear与清除浮动

    今天看bootstrap突然看到了 .container:after { clear: both; } 好像对clear的用法有点模糊,于是于是又研究一下用法. 上面搜资料总会搜到张鑫旭老师的相关文章 ...

  10. Xml解析过程中遇到“” 1 字节的 UTF-8 序列的字节 1 无效“”问题---idea与eclipse同适用

    转载自:http://blog.csdn.net/zhangzhikaixinya/article/details/7727938 今天在eclipse中编写pom.xml文件时,识别到错误:Inva ...