boost 绑定到数据成员回调行为
有人可以解释一下这段代码吗?
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()
却没有。
Can someone please explain this piece of code?
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);
Why does the definition of bar
compile and what is the result of it?
Edit: foo()
works as expected, calling c.member()
, but bar()
doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个调用用于“生成”提取函子。该函子在被调用时将返回它所绑定的成员。
第二个调用只是隐藏传入的仿函数的返回类型(与第一个示例中的相同)。所以本质上,调用
bar
不会执行任何操作。The first call is used to "generate" an extractor functor. That functor, when called, will return the member that it was bound to.
The second call just hides the return type of the functor that is passed in (which is the same as in the first example). So essentially, calling
bar
will do nothing.如果您的类是这样的,您将需要绑定:
class Class {
民众:
无效成员();
那么
你要做的是:
Class c;
boost::function; the_function_i_want_to_call = boost::bind(&Class::member, c);
the_function_i_want_to_call.call();
You would need to bind if your class was like that:
class Class {
public:
void member();
};
Then what you want to do is that :
Class c;
boost::function<void()> the_function_i_want_to_call = boost::bind(&Class::member, c);
the_function_i_want_to_call.call();