boost之function object

来源:互联网 发布:培训班java教哪些课程 编辑:程序博客网 时间:2024/06/06 00:23

struct write_five_obj 
{
void operator()() const
{
global_int = 5;
}
};

static void write_five()
{
global_int = 5;
}


//赋值的类型2种:

typedef function<void()> func_void_type;

write_five_obj five;

func_void_type v1;

v1 =  five;

v1 = write_five;


//可交换

swap(v1, v2);


//成员函数

struct X
{
X(int v) : value(v) {}
int twice() const { return 2 * value; }
int plus(int v) { return value + v;}

int value;
};

static void test_member_functions()
{
boost::function<int (X*)> f1(&X::twice);
X one(1);
X five(5);

std::cout << f1(&one) << " " << f1(&five) << std::endl;
}

原创粉丝点击