boost::function

一、介绍

boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象。它的概念很像广义上的回调函数。它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象。因此,它可以被用于回调机制,暂时保管函数或函数对象,在之后需要的时机在调用,使回调机制拥有更多弹性。回调函数的一种形式就是:C语言中的typedef 返回类型 (*pointer)(参数列表),如:

typedef boost::function<void(bool, int, int)> RequestCallback;

二、使用

头文件:

 #include<boost/function.hpp>
 using namespace boost;

示例:

int fsum(int i, int j)
{
    return i + j;
}

class Person
{
public:
    void operator() (std::string name, int age)
    {
        std::cout << name << ": " << age << std::endl;
    }
};

class Car
{
public:
    Car(){}
    virtual ~Car(){}
    void info(int i)
    {
        std::cout << "info = " << i << std::endl;
    }
};

void test_function()
{
    // 1. 普通函数
    boost::function<int(int, int)> func1;
    func1 = fsum;
    std::cout << "4 + 5 = " << func1(4, 5) << std::endl;

    // 2. 函数对象
    boost::function<void(std::string, int)> func2;
    Person person;
    func2 = person;
    func2("myname", 30);

    // 3. 成员函数
    boost::function<void(Car*, int)> func3;
    func3 = &Car::info;
    Car car;
    func3(&car, 25);

    // 4. 空函数
    boost::function<int(int, int)> func4;
    assert(func4.empty());
    assert(!func1.empty());
    func1.clear();
    assert(func1.empty());
    try
    {
        func1(4, 5);
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

三、高级用法

function可以配合bind使用,存储bind表达式的结果,使bind可以被多次调用,bind函数返回的是一个函数对象。
示例1:

#include "boost/bind.hpp" 

namespace 
{ 
  void overload(int param1, float param2, int param3) {} 
  void overload(int param) {} 
  void overload(float param) {} 

  class Class 
  { 
  public: // interface 
    void overload(float param) {} 
    void overload(int param) {} 
  }; 

} // namespace 

int main(int arg, char** argv) 
{ 
  // non-member 
  boost::bind(::overload, _1, _2, _3); // can bind normally 
  typedef void (*NonMemberFuncType)(int); 
  boost::bind(static_cast<NonMemberFuncType>(::overload), _1); 

  // member 
  Class* objPtr = new Class(); 
  typedef void (Class::*OverloadFuncType)(float); 
  boost::bind(static_cast<OverloadFuncType>(&Class::overload), objPtr, _1); 

} // main 

示例2:

#include "boost/function.hpp" 
#include "boost/bind.hpp" 

#include <string> 
#include <iostream> 

namespace 
{ 
  void function(int number, float floatation, std::string string) 
  { 
    std::cout << "Int: \"" << number << "\" Float: \"" << floatation 
              << "\" String: \"" << string << "\"" << std::endl; 
  } 

} // namespace 

int main(int c, char** argv) 
{ 
  // declare function pointer variables 
  boost::function<void(std::string, float, int)> shuffleFunction; 
  boost::function<void(void)> voidFunction; 
  boost::function<void(float)> reducedFunction; 

  // bind the methods 
  shuffleFunction = boost::bind(::function, _3, _2, _1); 
  voidFunction = boost::bind(::function, 5, 5.f, "five"); 
  reducedFunction = boost::bind(::function, 13, _1, "empty"); 

  // call the bound functions 
  shuffleFunction("String", 0.f, 0); 
  voidFunction(); 
  reducedFunction(13.f); 

} // main 

参考:
http://blog.csdn.net/fengbangyue/article/details/7185340
http://blog.csdn.net/huang_xw/article/details/8249278
http://www.radmangames.com/programming/how-to-use-boost-bind

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值