boost bind to a data member callback behavior
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'开发者_Python百科t.
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.
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();
精彩评论