提升绑定到数据成员回调行为

时间:2011-03-24 12:13:07

标签: boost boost-bind boost-function

有人可以解释这段代码吗?

struct Class {
    boost::function<void()> member;
};
Class c;

boost::function<boost::function<void()>()> foo = boost::bind(&Class::member, &c);
boost::function<void()> bar = boost::bind(&Class::member, &c);

为什么bar的定义会编译,结果是什么?

修改:foo()按预期工作,调用c.member(),但bar()没有。

2 个答案:

答案 0 :(得分:0)

如果您的班级是这样的话,您需要绑定:

class Class { public: void member(); };

然后你想要做的是:

Class c;

boost::function<void()> the_function_i_want_to_call = boost::bind(&Class::member, c);

the_function_i_want_to_call.call();

答案 1 :(得分:0)

第一个调用用于“生成”一个提取器仿函数。该函数在被调用时将返回它所绑定的成员。

第二个调用只隐藏传入的仿函数的返回类型(与第一个示例中的相同)。基本上,调用bar将不会做任何事情。

相关问题