Function Object in C++
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++的更多相关文章
- 【STL】-function object
// Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...
- 认识js函数对象(Function Object)
认识函数对象(Function Object) 可以用function关键字定义一个函数,对于每个函数可以为其指定一个函数名,通过函 数名来进行调用.这些都是代码给用户的印象,而在JavaScript ...
- Javascript中Function,Object,Prototypes,__proto__等概念详解
http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...
- flask_route错误:AttributeError: 'function' object has no attribute 'route'
问题: 路由完全正确,当只有一个名为home的函数处理这个路由时候,下一个路由处理函数,总是提示没有这个rotue属性 Traceback (most recent call last): File ...
- 函数对象与仿函数(function object and functor)
part 1. 仿函数在STL组件中的关系 如下图: # 仿函数配合算法完成不同的策略变化. # 适配器套接仿函数. part 2. 仿函数介绍 传递给算法的“函数型实参”不一定得是函数,可以是行为类 ...
- C++ 谓词(predicate) 与 仿函数 ( functor (function object))
谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a f ...
- tensorboardX使用中 AttributeError: 'function' object has no attribute 'graph'
最近在使用tensorboardX可视化网络结构,因为tensorboardX并非pytorch原生的可视化工具,所以版本之间并不兼容 在使用的过程中会遇到,AttributeError: 'func ...
- js常用数据类型(Number,String,undefined,boolean) 引用类型( function,object,null ),其他数据类型( 数组Array,时间Date,正则RegExp ),数组与对象的使用
js常用数据类型 数字类型 | 字符串类型 | 未定义类型 | 布尔类型 typeof()函数查看变量类型 数字类型 Number var a1 = 10; var a2 = 3.66; conso ...
- JavaScript:Function/Object/prototype/__proto__
console.log(Object.__proto__===Function.prototype); //true console.log(Object.prototype.__proto__); ...
随机推荐
- (Lua) C++ 寫函式,Lua 呼叫使用
C++ 裡頭寫一個 Function 但是透過 Lua 提供input參數並且回傳結果給Lua 輸出的辦法. Lua 檔案 , , ) print("ave : ", ave, & ...
- 爬虫之chrome浏览器的使用方法
chrome浏览器使用方法介绍 1. 新建隐身窗口 1.1 为什么需要新建隐身窗口 在打开隐身窗口的时候,第一次请求某个网站是没有携带cookie的,和代码请求一个网站一样,不携带cookie.这样就 ...
- python学习,day4:生成器
1.生成器:只有在调用是才会生成相应的数据.(比较省内存,它只保留当时生成的.而列表会保存整个列表) a = [i*2 for i in range(10)] #列表生成式 print(a) 这样会把 ...
- 逆向学习-Windows消息钩取
钩子 Hook,就是钩子.偷看或截取信息时所用的手段或工具. 消息钩子 常规Windows流: 1.发生键盘输入事件时,WM_KEYDOWN消息被添加到[OS message queue]. 2.OS ...
- css的reset和常用的html标签的默认样式整理
先看下常用的这些标签浏览器的默认样式有哪些: body{ margin: 8px;} hr{ border:1px inset; margin-top:.5em;margin-bottom:.5em; ...
- 【chrome】设置chrome允许WebGL从本地载入资源
找到chrome安装路径,然后创建一个快捷方式,右击该快捷方式,在 目标 输入框中加上-allow-file-access-from-files(前面加个空格),通过该快捷方式打开chrome就可以通 ...
- 下载windows版本apache网页服务器
1.进入apache服务器官网http://httpd.apache.org/,这里我们以下载稳定版的httpd 2.4.25为例,点击download. 2.点击链接Files for Micros ...
- HUE配置文件hue.ini 的pig模块详解(图文详解)(分HA集群和非HA集群)
不多说,直接上干货! 一.默认的pig配置文件 ########################################################################### ...
- python 四种单例模式
1 使用__new__方法 Python class Singleton(object): def __new__(cls, *args, **kw): if not hasattr(cls, '_i ...
- Struts2 ongl内存结构
valuestack是OgnlValueStack的实现,而OgnlValueStack是基于ValueStack的实现 valuestack的内存结构为: 里面主要的为:context和root r ...