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;
}
时间: 2024-07-29 02:15:31

boost::bind 与 boost::function 的使用方法例子的相关文章

boost::bind的使用方法

bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板. 用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不下去. 这里只记下它的用法: 9.1 对于普通函数 假如有函数 fun() 如下:  void fun(int x, int y) {   cout << x << ", " << y << endl;  } 现在我们看看怎么用 bind

关于boost::bind中fstream对象禁止拷贝的解决方法

fstream的拷贝构造函数是私有的,禁止fstream对象的拷贝. 比如,下面的程序编译出错,提示拷贝构造函数私有: #include<fstream> #include<iostream> #include<boost/thread/thread.hpp> using namespace std; void fun(ofstream &out) { std::cout<<"succeed!"<<endl; } in

boost::bind 学习

 最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多.    等我学习的东西慢慢的与平台无关的时候,可能可以写下来的东西又会慢慢多起来吧.....不过我想早一批订阅我博客的人应该已经不会再看了,因为已经很少会有程序语言或者C++之类的信息,相关的东西应该都是关于计算机图形,游戏的.或者,早一批订阅我博客的人现在也已经毕业,工作了呢?    对了,这次的主题是boost:bind.在历经了bo

c++11特性与cocos2d-x 3.0之std::bind与std::function

 原文地址:http://www.cnblogs.com/slysky/p/3822640.html 作者:阳光下的蒲公英 c++11特性与cocos2d-x 3.0之std::bind与std::function 昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0内的Label锚点是在ccp(0.5,0.5),而一直3.0rc0是ccp(0,

解析jQuery的三种bind/One/Live事件绑定使用方法_jquery

jQuery是 一款优秀的JavaScript框架,在旧版里主要用bind()方法,在新版里又多了两种One(),Live(),下面介绍这几种方法的使用: 1. bind/Unbind在jquery的事件模型中,有两个基本的事件绑 定函数,bind与unbind,这两个函数的含义就是匹配页面元素进行相关事件的处理.比如我们在JS中经常使用到的 onfocus,onblur,onmouseover,onmousedown等事件都可以作为bind的参数进行传递. $("#id").bind

jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解_jquery

本文实例分析了jQuery中bind(),live(),delegate(),on()绑定事件方法.分享给大家供大家参考,具体如下: 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅,也希望该文章日后能帮助到各位朋友,文中如有不当之处,还望各位指正,话不多说,直接进入正题. 一.bind() 简要描述 bind()向匹配元素添加一个或多个事件处理器. 使用方式 复制

ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法_php实例

本文实例讲述了ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法.分享给大家供大家参考.具体如下: 一.问题描述: ThinkPHP做文字水印 ,今天做一个电子请帖,就把祝福语贴到图片上面,发现一直报错是取不到图片类型,比如gif,jpg等,并提示call an undefined function exif_imagetype(). 二.解决方法: 出现这个错误就是php.in 配置问题,打开即可:打开扩展 exten

使用 Node.js 做 Function Test实现方法_javascript技巧

Info上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿.现在迫切需要个简单的模板项目能快速搭建function test. 后来我回去想了想,为什么我们非得用 java 来做 function test 呢? Node.js 应该是个不错的选择,并且对 json 有着天然的支持,于是回去在 github 上随手一搜,还果真有相关的项目:testosterone,于是便有了这篇blog. Server要做demo,

PHP错误WARNING: SESSION_START() [FUNCTION.SESSION-START]解决方法_php技巧

做开发的时候,操作session有时候会遇到这个问题:Warning: session_start() [function.session-start]-- 系统环境:WIN2003+IIS6+PHP5.2.12 PHP出现类似如下错误: Warning: session_start()[function.session-start]: ...... failed: No such file or directory......等情况, 可以偿试修改一下php.ini文件中的session.au