本文根据boost的教程整理。
主要介绍boost function对象的用法。
boost function
boost function是什么
boost function是一组类和模板组合,用于包装各种函数。从功能上,它类似于函数指针,但是比函数指针的功能更强大。
使用boost function,必须包含头文件
[cpp] #include
#include
除了头文件外,不需要额外的库。
注意,boost function有两种形式:一种为推荐形式;另外一种为可移植形式。推荐形式的语法更加简洁;可移植形式的可移植性好,但是语法罗嗦。由于目前的gcc/vc的版本都已经能够使用推荐形式了,因此,可移植形式就不在描述。有兴趣的可以参阅boost相关文档。
boost function 基本用法
例如,有一个函数
[cpp] float int_div(int x, int y)
{
return ((float)x)/y;
}
float int_div(int x, int y)
{
return ((float)x)/y;
}
我们可以这样使用
[cpp] oost::function
f = int_div;
std::cout<< f(5,3) << std::endl;
boost::function
f = int_div;
std::cout<< f(5,3) << std::endl;
可以看到,它的用法和函数指针很相似的。
当然,boost::function不止这些,请看下面的函数对象:
[cpp] struct int_add {
float operator()(int x, int y) {
return (float)(x + y);
}
};
struct int_add {
float operator()(int x, int y) {
return (float)(x + y);
}
};
上面的 boost::function
[cpp] f = int_add();
std::cout << "f add : "<< f(10,20) << std::endl;
f = int_add();
std::cout << "f add : "<< f(10,20) << std::endl;
另外,boost::function还可以用来判断函数是否为空
[cpp] f(f)
std::cout << " f is ok!"<< std::endl;
if(f)
std::cout << " f is ok!"<< std::endl;
要清空f,可以使用
[cpp] f = 0;
if(!f)
std::cout << "f is cleard!" << std::endl;
f = 0;
if(!f)
std::cout << "f is cleard!" << std::endl;
针对成员函数
成员函数,也可以被绑定,如有类
[cpp] struct X {
int foo(int x) {
std::cout << "X " << this << " foo x="<
};
};
struct X {
int foo(int x) {
std::cout << "X " << this << " foo x="<
};
};
可以这样使用
[cpp] boost::function
mf = &X::foo;
X x;
mf(&x, 5);
boost::function
mf = &X::foo;
X x;
mf(&x, 5);
和bind同时使用
在需要包装参数的场合,我们可以配合boost::bind一起使用。
首先,加入boost::bind的头文件
[cpp] #include
#include
这样使用
[cpp] boost::function
mbf = bind(&X::foo, &x, _1);
mbf(10);
boost::function
mbf = bind(&X::foo, &x, _1);
mbf(10);
bind将x的指针保存在function对象中。
function factory
function factory是一个封装类工厂的模板。它有两种,一种是value_factory,一种是factory。
[cpp]
boost::factory
// same as new T(arg1,arg2,arg3)
boost::value_factory
// same as T(arg1,arg2,arg3)
boost::factory
// same as new T(arg1,arg2,arg3)
boost::value_factory
// same as T(arg1,arg2,arg3)
使用function factory的原因
我们考虑这样的场景:使用抽象工厂模式,有一个接口,有若干个实现,通常的做法是这样的:
[cpp]
//声明接口
class Interface
{
public:
virtual void print(int a) = 0; //接口函数
};
//声明抽象工厂
class Interface_Factory {
public:
virtual Interface * create() = 0;
};
//声明接口
class Interface
{
public:
virtual void print(int a) = 0; //接口函数
};
//声明抽象工厂
class Interface_Factory {
public:
virtual Interface * create() = 0;
};
然后,我们有若干个实现
[cpp]
class ImplA : public Interface
{
public:
virtual void print(int a) {
std::cout << "== A == a=" << a << std::endl;
}
};
//提供ImplA的类工厂
class ImplA_Factory : public Interface_Factory
{
public:
Interface * create() { return new ImplA(); }
static ImplA_Factory _implAFactory;
};
ImplA_Factory ImplA_Factory::_implAFactory;
//同理,ImplB的实现
class ImplB : public Interface
{
public:
virtual void print(int a) {
std::cout << "== B == a=" << a << std::endl;
}
};
//提供ImplB的类工厂
class ImplB_Factory : public Interface_Factory
{
public:
Interface * create() { return new ImplB(); }
static ImplB_Factory _implBFactory;
};
ImplB_Factory ImplB_Factory::_implBFactory;
class ImplA : public Interface
{
public:
virtual void print(int a) {
std::cout << "== A == a=" << a << std::endl;
}
};
//提供ImplA的类工厂
class ImplA_Factory : public Interface_Factory
{
public:
Interface * create() { return ne