boost::bind 与 boost::function 的使用方法例子
啥也不说,直接看代码:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>using namespace boost;
using namespace std;int f(int a, int b)
{return a + b;
}int g(int a, int b, int c)
{return a + b * c;
}class Point
{
public:Point(int x, int y) : _x(x), _y(y) {}void print(const char *msg) {cout << msg << "x=" << _x << ", y=" << _y << endl;}
private:int _x, _y;
};int main()
{//! f 有多少个参数,f 后面就要跟多少个参数。如果不明确的,用占位符cout << bind(f, 2, 3)() << endl; // ==> f(2, 3)cout << bind(f, 12, _1) (5) << endl; // ==> f(12, 5),其中参数b为不明确参数cout << bind(g, _2, _1, 3) (3, 5) << endl; // ==> g(5, 3, 3) 注意顺序Point p(11, 34);Point &rp = p;Point *pp = &p;bind(&Point::print, p, "Point: ") ();// ^ ^// 注意,为表示Point::print为成员函数,成员函数前面要加&区分。// 对象可以为实例、引用、指针bind(&Point::print, rp, "Reference: ") (); //! 引用bind(&Point::print, pp, _1) ("Pointer: "); //! 指针bind(&Point::print, _1, _2) (p, "As parameter: "); //! 对象也可以用占位符暂时代替//! function的类型定义与需要的参数有关function<void ()> func0 = bind(&Point::print, p, "func0: ");function<void (const char *)> func1 = bind(&Point::print, pp, _1);func0(); //! function对象的调用func1("func1: ");return 0;
}
一般情况下,bind 与 function 配合使用。
bind与function还可以将类型完成不同的函数(成员函数与非成员函数)包装成统一的函数调用接口。如下示例:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <vector>using namespace boost;
using namespace std;class Point
{
public:Point(int x, int y) : _x(x), _y(y) {}void print(string msg) {cout << msg << "x=" << _x << ", y=" << _y << endl;}
private:int _x, _y;
};class Person
{
public:Person(string name, int age) : _name(name), _age(age) {}void sayHello() {cout << "Hello, my name is " << _name << ", my age is : " << _age << endl;}
private:string _name;int _age;
};void printSum(int limit)
{int sum = 0;for (int i = 0; i < limit; ++i) {sum += i;}cout << "sum = " << sum << endl;
}void doAll(vector<function<void ()> > &funcs)
{for (size_t i = 0; i < funcs.size(); ++i) {funcs[i] ();}
}int main()
{vector<function<void ()> > funcList;Person john("John", 23);funcList.push_back(bind(&Person::sayHello, john));Point p1(23, 19);funcList.push_back(bind(&Point::print, p1, "Point: "));funcList.push_back(bind(printSum, 20));doAll(funcList);return 0;
}
boost::bind 与 boost::function 的使用方法例子相关推荐
- Boost.Bind的基础使用
当我们使用函数时习惯于C函数的格式,即如下形式 resulttype funname( arglist ); 返回值类型 函数名( 参数列表 ); 在Boost.Function中,我们可以方便的定义 ...
- Boost::Bind 基础
2019独角兽企业重金招聘Python工程师标准>>> 先了解一下:函数对象 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对 ...
- boost bind使用指南
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板. 用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看 ...
- Boost:bind绑定的function<>测试程序
Boost:bind绑定的function<>测试程序 实现功能 C++实现代码 实现功能 bind绑定的function<>测试程序 C++实现代码 #include < ...
- 【Boost】以boost::function和boost:bind取代虚函数
这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boos ...
- boost::function和boost:bind取代虚函数
这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boos ...
- c++ Boost库之boost::bind学习
刚开始学c++,就看boost库其实有点小小的不情愿. 团队要求必掌握的Boost库: boost::bind boost::function boost::Signals2 学习前奏:在写关于coc ...
- 展示使用 boost bind 和 phoenix 处理删除的不同方法的测试程序
展示使用 boost bind 和 phoenix 处理删除的不同方法的测试程序 实现功能 C++实现代码 实现功能 展示使用 boost bind 和 phoenix 处理删除的不同方法的测试程序 ...
- Boost:boost :: bind相等运算符的测试程序
Boost:boost :: bind相等运算符的测试程序 实现功能 C++实现代码 实现功能 boost :: bind相等运算符的测试程序 C++实现代码 #include <boost/c ...
- ROS下的多参数调用,boost::bind使用
主要是当我们订阅一个消息时候,会调用一个返回函数. 例如: 1 ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/te ...
最新文章
- 百度研究院再升级,迎来9位世界级科学家
- Fabric--CA 应用与配置
- 【SPSS】SPSS第五周作业
- Ambari 2.6.0 HDP 2.6.3集群搭建
- Ubuntu安装完后设置root密码-转
- “21天好习惯”第一期-9
- 20155337 《网络对抗》 Exp2 后门原理与实践
- IE浏览器起始页通过注册表修改
- 互联网大厂薪资最全揭秘:华为
- 宽带速率单位《Mbps-MBps/换算》
- 知识树软件的IPO图
- Python经典编程习题100例:第56例:画图,学用circle画圆形
- 网上传的京东撸货为什么那么火?
- 功能强大的截图软件——Snipaste
- Python OCR 识别图片内容
- 如何在Mac和Windows PC之间无线共享文件
- html表格方式实现商品详情
- 12.5-6黄金实时指导、黄金原油操作策略及多空单解套
- 程序员转行能做什么?
- java 15k_月薪15K的Java工程师必备的十大技能
热门文章
- paip.获取文件名从路径uapi java python php总结...
- paip.C#.NET多线程访问 toolStripStatusLabel VC421
- paip.流程图的图形化编程及源码生成时序图
- 着力财富管理市场产品全覆盖 基金公司争设销售子公司
- 一文读懂 K8s 持久化存储 | 凌云时刻
- 259年后,中国最大的皇家园林上云了
- Gartner 2020年十大战略科技发展趋势:边缘赋能、区块链、超自动化、人工智能安全等...
- 【手写字母识别】基于matlab GUI模板匹配手写大写字母识别【含Matlab源码 115期】
- 【优化算法】烟花优化算法(FWA)【含Matlab源码 1079期】
- 【图像边缘检测】基于matlab拉普拉斯算法图像边缘检测与增强【含Matlab源码 456期】