Creating a functor inside a member function without taking the class as an argument

Apologies for cryptographic decryption.

I want 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;

      

Is it possible something like

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

      

with boost :: bind or something else?

thank

+3


source to share


1 answer


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

as an argument.

You can accomplish this with boost::bind

:



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

      

+4


source







All Articles