void someFunction(boost::function<void()> func)
{
... //Get myObj
}
MyClass *myObj = ...;
someFunction(boost::bind(&MyClass::memberFunction, myObj));
How can I get pointer or reference to myObj from inside the function void someFunction
void someFunction(boost::function<void()> func)
{
... //Get myObj
}
MyClass *myObj = ...;
someFunction(boost::bind(&MyClass::memberFunction, myObj));
How can I get pointer or reference to myObj from inside the function void someFunction
Generally it is not possible nor desirable to extract the object used as an argument to
boost::bind
(orstd::bind
) back from the result of the bind. The way the resulting object is stored is implementation specific.Observe how it is defined as unspecified in the documentation (see link below):
To illustrate further, take a look at this paragraph, on the same page:
http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html#CommonDefinitions
The library developers explicitly tell you that the type that's returned is opaque, won't give you back the type of the arguments you've passed in, and don't intend for you to obtain the objects from within the opaque bind return type.
However, when you call bind() it is you who supplies the argument, so you can just store them aside and use them later. Alternatively, as suggested in the comments you can just use
*this
as a reference to the callee when the bound method is invoked.