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  2 {
 3  3     int sum(0);
 4  4     for (int i = 0; i < n; ++i)
 5  5     {
 6  6         sum += values[i];
 7  7     }
 8  8     cout << sum << endl;
 9  9 };
10 10 int _tmain(int argc, _TCHAR* argv[])
11 11 {
12 12     boost::function<void(int *values, int n)> sum;
13 13     sum = &do_sum;
14 14     int a[] = {1,2,3,4,5};
15 15     sum(a, 5);
16 16     return 0;
17 17 }

sum 可以理解为一个广义的函数对象了,其只用就是保存函数do_sum, 然后再调用之。

2.2 成员函数

在很多系统中, 对于类的成员函数的回调需要做特殊处理的。这个特殊的处理就是“参数绑定”。当然这个超出了我们讨论的范围了。 boost::function对于成员函数的使用可以看下如下代码:

 1 class X{
 2  2 public:
 3  3     int foo(int a)
 4  4     {
 5  5         cout << a <<endl;
 6  6         return a;
 7  7     }
 8  8 };
 9  9 int _tmain(int argc, _TCHAR* argv[])
10 10 {
11 11     boost::function<int(X*, int)>f;
12 12     f = &X::foo;
13 13     X x;
14 14     f(&x, 5);
15 15     return 0;
16 16 }

我们发现, 对类的成员函数的对象化从语法是没有多大的区别。

3. 一个典型的例子

上面的几个例子没有体现出boost::function的作用来, 这里在写一个例子。比如当程序执行到某一处的时候想绑定某一个函数, 但是不想立即执行, 我们就可以声明一个函数对象,给此对象绑定相应的函数, 做一些其他事情,然后再来执行绑定的函数, 代码如下:

 1 void print(int a)
 2  2 {
 3  3     cout << a << endl;
 4  4 }
 5  5 typedef boost::function<void (int)> SuccessPrint;
 6  6 int _tmain(int argc, _TCHAR* argv[])
 7  7 {
 8  8     vector<SuccessPrint> printList;
 9  9     SuccessPrint printOne = boost::bind(print, _1);
10 10      printList.push_back(printOne);
11 11     SuccessPrint printTwo = boost::bind(print, _1);
12 12     printList.push_back(printTwo);
13 13     SuccessPrint printThree = boost::bind(print, _1);
14 14     printList.push_back(printTwo);
15 15     // do something else
16 16     for (int i = 0; i < printList.size(); ++i)
17 17         printList.at(i)(i);
18 18     return 0;
19 19 }

上述代码中首先把声明一个函数对象 typedef boost::function<void (int)> SuccessPrint, 然后把print绑定到斥对象中, 放入vector中, 到最后才来执行这print()函数。

boost::function的用法(一)相关推荐

  1. boost::function的用法(二)

    boost function是一组类和模板组合,用于包装各种函数.从功能上,它类似于函数指针,但是比函数指针的功能更强大. 使用boost function,必须包含头文件 [cpp] view pl ...

  2. boost function对象

    本文根据boost的教程整理. 主要介绍boost function对象的用法. boost function boost function是什么 boost function是一组类和模板组合,用于 ...

  3. boost::function用法的测试程序

    boost::function用法的测试程序 实现功能 C++实现代码 实现功能 boost::function用法的测试程序 C++实现代码 #include <boost/core/ligh ...

  4. boost::function模块boost::lambda::bind用法的测试程序

    boost::function模块boost::lambda::bind用法的测试程序 实现功能 C++实现代码 实现功能 boost::function模块boost::lambda::bind用法 ...

  5. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  6. 【Boost】boost库中function的用法

    要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从"boost/function/funct ...

  7. boost::function用法详解

    要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...

  8. boost::function30的用法实例

    boost::function30的用法实例 实现功能 C++实现代码 实现功能 boost::function30的用法实例 C++实现代码 #include <boost/function/ ...

  9. 【Boost】以boost::function和boost:bind取代虚函数

    这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boos ...

最新文章

  1. Github上 10 个超好看可视化面板
  2. java如何定义一个字符栈_Java性能优化之字符串优化处理
  3. C++中#ifndef/#define/#endif使用详解
  4. 3D电视,你知道多少?
  5. java三目表达式_Java8新特性Lambda表达式
  6. Too many open files 问题
  7. 全球地区资料json 含中英文 经纬度_2020年Brain Bee北京、天津、河北赛区地区赛参赛说明...
  8. 来讨论一下这些常见的 Redis 面试题
  9. Ajax Accordion(可折叠) 动态生成菜单
  10. hdu 1333水题
  11. vc++之剪贴板通信实例
  12. ThinkPHP 3.2.3方法函数总结
  13. 转 未能使用提供程序 RsaProtectedConfigurationProvider 进行解密 的解决办法
  14. VB.NET 网络通讯示例(服务端)
  15. jQuery实现form表单reset按钮重置清空表单功能
  16. 车牌识别系统 HY-LPR2
  17. 四、Raid卡(阵列卡)
  18. DRM GEM 驱动程序开发(dumb)
  19. 在Illustrator和手绘文章中创建矢量图形
  20. 关于三菱触摸屏GT Designer3 仿真软件创建工程

热门文章

  1. 开源中国 2014 年源创会年度计划
  2. 【Construct Binary Tree from Inorder and Postorder Traversal】cpp
  3. php 数据类型转换与比较
  4. C#多线程JOIN方法初探
  5. 服务器内存延迟,内存带宽、延迟性能测试
  6. 编译器入门 语法分析器 java_从零开始写个编译器吧 - Parser 语法分析器
  7. fib函数用python编写_Python中利用函数装饰器实现备忘功能
  8. mvn 打包_Spark源码打包编译的过程
  9. STM32串口通信中使用printf发送数据配置方法 开发环境 Keil
  10. Machine Learning(Stanford)| 斯坦福大学机(吴恩达)器学习笔记【汇总】