Creation of a functor inside a member function without taking the class as a argument

167 views Asked by At

Apologies for the cryptic decryption.

I wish to create a functor of the following type:

const boost::function<bool ()>& functor

Please consider the class:

#include <boost/function.hpp>
class X { 
    public:
        bool foo();
        void bar() ;
};

void X::bar() {
    const boost::function<bool (X *)>& f = &X::foo;
}

bool X::foo() {
    std::cout << __func__ << " " << __LINE__ << " " << std::endl;
    return true;
}

I have:

const boost::function<bool (X *)>& f = &X::foo;

Can I have something like

const boost::function<bool ()>& f = &X::foo;

with boost::bind or something else ?

Thanks

1

There are 1 answers

2
101010 On BEST ANSWER

A non-static member function must be called with an object. Thus, you must always implicitly pass this pointer as its argument.

You can accomplish this with boost::bind:

const boost::function<bool()>& f = boost::bind(&X::foo, this);