boost::function 介绍
本片文章主要介绍boost::function的用法。 boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象。
1. 介绍
Boost.Function 库包含了一个类族的函数对象的包装。它的概念很像广义上的回调函数。其有着和函数指针相同的特性但是又包含了一个调用的接口。一个函数指针能够在能以地方被调用或者作为一个回调函数。boost.function能够代替函数指针并提供更大的灵活性。
2. 使用
Boost.Function 有两种形式:首选形式和便携式形式, 其语法如下:
| 首选形式 | 便携式形式 |
| boost::function<float(int x, int y)>f | boost::function2<float, int, int>f |
但是便携式形式不是所有的编译器都支持的, 所以这里我只介绍首选形式。
2.1 普通函数
我们可以看下如下的例子:
1 void do_sum(int *values, int n)
2 {
3 int sum(0);
4 for (int i = 0; i < n; ++i)
5 {
6 sum += values[i];
7 }
8 cout << sum << endl;
9 };
10 int _tmain(int argc, _TCHAR* argv[])
11 {
12 boost::function<void(int *values, int n)> sum;
13 sum = &do_sum;
14 int a[] = {1,2,3,4,5};
15 sum(a, 5);
16 return 0;
17 }
sum 可以理解为一个广义的函数对象了,其只用就是保存函数do_sum, 然后再调用之。
2.2 成员函数
在很多系统中, 对于类的成员函数的回调需要做特殊处理的。这个特殊的处理就是“参数绑定”。当然这个超出了我们讨论的范围了。 boost::function对于成员函数的使用可以看下如下代码:
1 class X{
2 public:
3 int foo(int a)
4 {
5 cout << a <<endl;
6 return a;
7 }
8 };
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 boost::function<int(X*, int)>f;
12 f = &X::foo;
13 X x;
14 f(&x, 5);
15 return 0;
16 }
我们发现, 对类的成员函数的对象化从语法是没有多大的区别。
3. 一个典型的例子
上面的几个例子没有体现出boost::function的作用来, 这里在写一个例子。比如当程序执行到某一处的时候想绑定某一个函数, 但是不想立即执行, 我们就可以声明一个函数对象,给此对象绑定相应的函数, 做一些其他事情,然后再来执行绑定的函数, 代码如下:
1 void print(int a)
2 {
3 cout << a << endl;
4 }
5 typedef boost::function<void (int)> SuccessPrint;
6 int _tmain(int argc, _TCHAR* argv[])
7 {
8 vector<SuccessPrint> printList;
9 SuccessPrint printOne = boost::bind(print, _1);
10 printList.push_back(printOne);
11 SuccessPrint printTwo = boost::bind(print, _1);
12 printList.push_back(printTwo);
13 SuccessPrint printThree = boost::bind(print, _1);
14 printList.push_back(printTwo);
15 // do something else
16 for (int i = 0; i < printList.size(); ++i)
17 printList.at(i)(i);
18 return 0;
19 }
上述代码中首先把声明一个函数对象 typedef boost::function<void (int)> SuccessPrint, 然后把print绑定到斥对象中, 放入vector中, 到最后才来执行这print()函数
boost::function 介绍的更多相关文章
- boost::function和boost::bind 介绍
一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...
- boost::function的用法
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- boost function对象
本文根据boost的教程整理. 主要介绍boost function对象的用法. boost function boost function是什么 boost function是一组类和模板组合,用于 ...
- Boost Asio介绍--之一
原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39 CSDN博客 原文 http:/ ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...
- 关于boost::function与boost::bind函数的使用心得
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...
- [转] boost::function用法详解
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...
随机推荐
- eclipse编译hbase 1.3.1(转)
https://yq.aliyun.com/articles/59830 ,晚上回去试试...
- 面向对象【day07】:面向对象使用场景(十)
本节内容 1.概述 2.知识回顾 3.使用场景 一.概述 之前我们学了面向对象知识,那我们在什么时候用呢?不可能什么时候都需要用面向对象吧,除非你是纯的面向对象语言,好的,我们下面就来谈谈 二.知识回 ...
- input:checked + label用法
input:checked ~ label :相邻同胞选择器,选择被选中的input标签后 所有的label标签[input 和 label标签有共同的父元素]: input:checked + ...
- Kafka技术内幕 读书笔记之(一) Kafka入门
在0.10版本之前, Kafka仅仅作为一个消息系统,主要用来解决应用解耦. 异步消息 . 流量削峰等问题. 在0.10版本之后, Kafka提供了连接器与流处理的能力,它也从分布式的消息系统逐渐成为 ...
- CNPM
cnpm 代替 npm npm install -g cnpm --registry=https://registry.npm.taobao.org Reference 淘宝 NPM 镜像
- python+selenium 模拟登陆,自动下单
目前写的实在太粗糙,留着,以后来写上
- java操作数据库:分页查询
直接上.... 还是用之前的goods表,增加了一些数据 1.实体类Goods // 封装数据 public class Goods { private int gid; private String ...
- [译]Nuget.Server
原文 NuGet.Server是一个包,可用于使一个ASP.NET应用host一个package feed . 使用VS创建一个新的空WEB应用,添加Nuget.Server包. 配置应用的Packa ...
- [译]ES6特性
原文 作用域 使用let/const替代var. var有什么错? var说明的变量会产生作用于提升的概念. ES5: var x = 'outer'; function test(inner) { ...
- kali渗透测试之缓冲区溢出实例-windows,POP3,SLmail
kali渗透测试之缓冲区溢出实例-windows,POP3,SLmail 相关链接:https://www.bbsmax.com/A/xl569l20Jr/ http://4hou.win/wordp ...