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. TeamView 连接2、3事

    问题1: 客户通过本地远程上服务器开TeamView让我们连,我们连上后发现开户一把她的远程关掉就卡住了. 如图,原来TeamView会启动多用户增强支持. 原来用的是用户ID连的,用户断掉远程后就不 ...

  2. 没事用html5 canvas画一个仪表盘自用,自适应的哦

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. [Xamarin.Android] 儲存資料於Windows Azure (转帖)

    在準備討論Xamarin.Android 如何整合GCM與Windows Azure來實作Push Notification之前, 先來了解如何將Xamarin.Android 與Windows Az ...

  4. ZendStudio操作技巧

    1.恢复窗口默认布局 点开菜单栏上的“windows”,出来的菜单中有个“Reset Perspective...”,点这个就行了

  5. 几个用Python实现的简单算法

    一.算法题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源 ...

  6. PHP之mb_check_encoding使用

    mb_check_encoding (PHP 4 >= 4.4.3, PHP 5 >= 5.1.3, PHP 7) mb_check_encoding - Check if the str ...

  7. 错误:‘lock_guard’ 在此作用域中尚未声明

    解决:修改报错文件,加入#include <boost/thread/lock_guard.hpp>

  8. tomcat原理解析(二):整体架构

    一 整体结构 前面tomcat实现原理(一)里面描述了整个tomcat接受一个http请求的简单处理,这里面我们讲下整个tomcat的架构,以便对整体结构有宏观的了解.tomat里面由很多个容器结合在 ...

  9. Abschlussarbeit:Konstruktion und Implementierung von Dota2 Datenbank Intelligent Verwaltungsplatfom

    1.Die Hintergrund und Bedeutung des Themas Dank nicht ausreichendes Erkenntnisse der Spielplanner un ...

  10. C# 通过反射获取方法/类上的自定义特性

    1.所有自定义属性都必须继承System.Attribute 2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾 自定义属性QuickWebApi [Attribu ...