1
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

Saif Ur Rehman
  • 338
  • 3
  • 14
  • why don't you simply pass in ref to object? – billz Jan 11 '14 at 04:16
  • @billz Do you mean passing it as a second argument after boost::function func? I'm trying to make a boost::signals type connect function. Boost doesn't require passing myObj twice. Its mainly to keep the parameter short. – Saif Ur Rehman Jan 11 '14 at 04:30
  • It's not clear what you're trying to accomplish but once parameter have been bound to a function, you can't get to them. – Eric Fortin Jan 11 '14 at 04:33
  • @EricFortin I'm not talking about how to get the parameter, I'm asking how I can get the object that is used to call the member function. boost::bind(&MyClass::memberFunction, myObj)() would call myObj->memberfunction(), how can I get the pointer or reference of myObj – Saif Ur Rehman Jan 11 '14 at 04:52
  • Well myObj is the first parameter passed to your member function. This is how member function work under the hood. – Eric Fortin Jan 11 '14 at 04:55

1 Answers1

1

Generally it is not possible nor desirable to extract the object used as an argument to boost::bind (or std::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):

// one argument
template<class R, class F, class A1> unspecified-3 bind(F f, A1 a1);

To illustrate further, take a look at this paragraph, on the same page:

The function objects that are produced by boost::bind do not model the STL Unary Function or Binary Function concepts, even when the function objects are unary or binary operations, because the function object types are missing public typedefs result_type and argument_type or first_argument_type and second_argument_type. In cases where these typedefs are desirable, however, the utility functionmake_adaptable can be used to adapt unary and binary function objects to these concepts.

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.

#include <boost/bind.hpp>
#include <iostream>

struct Foo {
    void f() const {
       const Foo& myObj = *this;
       std::cout << "Invoked  instance: " << std::hex << &myObj << std::endl;
    }
};

int main() {
    Foo foo;
    std::cout << "Invoking instance: " << std::hex << &foo << std::endl;
    boost::bind(&Foo::f, boost::ref(foo))();
    return 0;
}
/* Output:
Invoking instance: 0x7fff4381185f
Invoked  instance: 0x7fff4381185f */
mockinterface
  • 14,452
  • 5
  • 28
  • 49