1

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

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()没有。

4

2 回答 2

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();

于 2011-03-24T13:00:14.370 回答
0

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

第二个调用只是隐藏了传入的函子的返回类型(与第一个示例相同)。所以本质上,调用bar不会做任何事情。

于 2011-03-30T14:38:04.643 回答