boost::function总结

编写时间:2018年10月23日15:13:11



使用情况

1: 简单的函数指针

#include <boost/function.hpp>
#include <stdio.h>

int getMax(int a,int b)
{
  return a > b ? a : b;
}

int main()
{
  boost::function<int(int,int)> f1;
  f1 = &getMax;
  printf("%d\n",f1(1,2));

  int (*f2)(int,int);
  f2 = &getMax;
  printf("%d\n",f2(1,2));
}

以上两种使用情况是等价的

2: 简单的对象关联函数指针

#include <boost/function.hpp>
#include <stdio.h>
#include <vector>

class nofitier
{
  typedef boost::function<void(int&)> function_type;
  std::vector<function_type> _vec;
  int _value;
public:
  nofitier()
    : _value(10) { }

  template <typename T> void add_observer(T t)
  {
    _vec.push_back(function_type(t));
  }

  void change_value()
  {
    for(std::size_t i = 0; i < _vec.size(); ++i)
    {
      _vec[i](_value);
    }
  }
};

void print_new_value(int &n)
{
  printf("The function name is print_new_value!\n \
          The class name is nofitier\n \
          The value is %d\n",n);
}

void interested_in_the_change(int &n)
{
  n = n + 17;
}

struct knows_the_previous_value
{
  void operator()(int &n)
  {
    printf("The function name is knows_the_previous_value!\n \
            The class name is nofitier\n \
            The value is %d\n",n);
  }
};

int main()
{
  nofitier n;
  n.add_observer(&print_new_value);
  n.add_observer(&interested_in_the_change);
  n.add_observer(knows_the_previous_value());
  n.change_value();

  return 0;
}

3: 类函数指针

class some_class
{
public:
  void do_stuff(int i) const
  {
    std::cout << "OK. Stuff is done. " << i << std::endl;
  }
};

int main()
{
  // 通过值传递需要执行的对象
  boost::function<void(some_class,int)>  f1;
  // 通过引用传递需要执行的对象
  boost::function<void(some_class&,int)> f2;
  // 通过指针传递需要执行的对象
  boost::function<void(some_class*,int)> f3;
/***
  注意,对于传值而言,如果类比较小而且函数并不需要修改的时候,按值传递比较安全。
  而对于传引用和传指针,实际上是一样的,都是传入当前函数的指针,
  只是引用不能与c兼容,而传指针可以很好的和c语言兼容。
*/

  some_class sc;
  f1 = &some_class::do_stuff;
  f1(sc,2);
  f2 = &some_class::do_stuff;
  f2(sc,2);
  f3 = &some_class::do_stuff;
  f3(&sc,2);
}

4: 带状态的类对象

#include <boost/function.hpp>
#include <stdio.h>
#include <vector>
#include <iostream>

struct some_class_with_opt
{
  int _value;

  some_class_with_opt()
    : _value(1) {}

  void operator()()
  {
  	_value += 10;
    printf("The function name is %s\n \
            Now the value is %d\n", __func__, _value);
  }
};

int main()
{
  boost::function<void()> f1;
  boost::function<void()> f2;

  some_class_with_opt v1;
  f1 = v1;
  f2 = v1;

  f1();
  f2();
  printf("Now the class's value is %d\n", v1._value);
}

以上运行结果如下:

The function name is operator()
             Now the value is 11
The function name is operator()
             Now the value is 11
Now the class's value is 1

可以看出boost::function的复制函数是按值传递的,所以看起来并没有对函数内部的值发生什么改变,以下可以这么改。

  f1 = boost::ref(v1);
  f2 = boost::ref(v1);

运行结果如下:

The function name is operator()
             Now the value is 11
The function name is operator()
             Now the value is 21
Now the class's value is 21

参考链接:
https://blog.youkuaiyun.com/benny5609/article/details/2324474

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值