Boost程序库学习-bind与function
来源:互联网 发布:大英帝国 知乎 编辑:程序博客网 时间:2024/04/28 11:50
平时我们使用函数指针与函数对象的时候,需要定义:typedef void (*Func)(int,int);
代码的可读性很差,boost::bind有如一个适配器,可以调用普通函数、成员函数、函数对象。而结合function可以作为一个回调函数。
所谓函数对象就是一个重载了operator()
操作符的对象 。
1.bind
先见一个简单的例子:
#include <boost/assign.hpp>#include <boost/bind.hpp>#include <boost/foreach.hpp>#include <boost/function.hpp>#include <boost/ref.hpp>#include <boost/typeof/typeof.hpp>#include <boost/utility/result_of.hpp>#include <iostream>#include <math.h>#include <typeinfo>using namespace std;using namespace boost;using namespace boost::assign;int addTest(int a, int b){ return a + b;}void test1(){ //定义函数指针 typedef int (*Func1)(int x, int y); //声明变量 Func1 func1 = addTest; func1(3,5); cout<<bind(addTest,_1,_2)(3,5)<<endl;}
第一种情况是使用函数指针来调用函数,第二种方式使用boost的bind,可以达到一样的效果。
原理:bind(addTest,_1,_2),_1,_2位占位符,这里的占位符可以不分先后,由后面的实参决定。bind()过程中,存储addTest,_1,_2的拷贝,得到一个临时的无参的函数对象,当operator()调用发生时,接受真正的参数,将_1,_2传递给addTest函数。
bind可以绑定普通函数,函数对象与成员函数:
1 普通函数
void print(int x){ cout << "test" << x << endl;}void bindTest1(){ //普通函数的绑定 typedef void (*Print1)(int); Print1 t1 = print; t1(1); //函数指针的调用 typedef boost::function<void(int)> Func; // Func func1=t1;//将函数指针赋值于function Func func1 = bind(print, _1); func1(2);}
2 成员函数
struct point{ typedef int result_type; int x, y; point(int a = 0, int b = 0) : x(a), y(b) {} void print(int z) { x = x + y + z; cout << "(" << x << "," << y << ")" << endl; } int operator()(int z) { x = x + y + z; cout << x << " " << y << endl; return 0; } int add(int x, int y) { return x + y; }};
该类中重载了operator()
,所以这是一个函数对象。
void bindTest2(){ //成员函数的绑定 vector<point> v(10); for_each(v.begin(), v.end(), bind(&point::print, _1, 1));}
其中for_each
为boost中的标准算法 ,占位符_1,传入向量中的point
对象 .bind成员函数不能直接调用operator(),因此必须传入对象地址,得到this指针才能在栈中调用成员函数,1为参数。
3 函数对象
void bindTest3(){ vector<point> v2(5); for_each(v2.begin(), v2.end(), bind(point(), 1));}
2. function
是函数对象容器,以对象的形式封装原始函数指针活函数对象,可以暂时保管函数活函数对象,等到需要使用的时候再进行调用。与bind的结合,可以多次调用函数。
简单例子:
void functionTest(){ //function demo function<int(int, int)> func; func = f; cout << func(10, 2) << endl; //bind demo cout << bind(f, _1, _2)(10, 2) << endl; //成员函数调用 function<int(point &, int, int)> func1; func1 = bind(&point::add, _1, _2, _3); point p; cout << func1(p, 10, 32) << endl; function<int(int, int)> func2; func2 = bind(&point::add, p, _1, _2); cout << func2(38, 2) << endl;}
2) 适用于回调函数
class test_fun{ private: int data1; int data2; public: test_fun(int x, int y) : data1(x), data2(y) {} typedef function<int(int, int)> Func; Func fun; template <typename CallBack> void assignFun(CallBack f) { fun = f; } void doWork() { cout << "data is:" << fun(data1, data2) << endl; }};int call_back_fun(int a, int b){ return a - b;}int call_back_fun1(int a,int b,int c){ return a+b-c;}void callbacktest(){ test_fun demoClass(23, 4); demoClass.assignFun(call_back_fun); demoClass.doWork(); //以绑定的方式调用函数 demoClass.assignFun(bind(call_back_fun1,_1,_2,4)); demoClass.doWork();}
采用function,一个模板可以接受多个函数,调用时机可灵活的处理。
0 0
- Boost程序库学习-bind与function
- boost::function与boost::bind 的使用
- [C++] boost::function boost::bind 与 回调
- boost::bind\boost::function
- boost::bind/boost::function
- C++ boost bind与 boost function的使用
- 关于boost::function与boost::bind函数的使用心得
- 关于boost::function与boost::bind函数的使用心得
- boost function bind用法
- boost : ref , bind , function
- boost::function和boost::bind
- boost::function 和boost::bind
- Boost程序库学习-foreach
- Boost程序库学习-fileSystem
- boost程序库学习-lexical_cast
- boost库之bind(与function配合使用)
- boost------bind的使用(Boost程序库完全开发指南)读书笔记
- boost bind function用法说明
- Android语言国际化values资源文件命名规则
- 安卓 activity过渡动画
- 使用ApkTool以及dex2jar对apk进行反编译-更新异常以及解决方案
- 透明栏颜色改变
- log4j配置详解
- Boost程序库学习-bind与function
- Android内存泄漏的各种原因详解
- 1.7前置声明与定义
- JavaSE学习_08_abstract_interface_innerclass_constantpool
- 经典排序算法归纳笔记(1)
- Android自定义View初体验,实现圆形TextView的三种方式
- 线性回归LinearRegress
- 【JAVA 并发】一 同步访问共享的可变数据
- 经典排序算法归纳笔记(2)