Re: [sigc] bind arguments within mem_fun()



Am 01.09.2004 15:50:51 schrieb(en) Andreas Matthias:
Is it possible to bind an argument to a function that is referenced
by mem_fun()?

Yes - you can use sigc::bind() (see below).

Actually I want to achieve the effect of
   sigc::mem_fun(*this, &A::foo)(99);
but I need to bind 99 to &A::foo, since I need a nullary function
call.

Below you find an example exhibiting the problem.

Ciao
Andreas


#include <sigc++/sigc++.h>
#include <boost/bind.hpp>

struct A
{
    A()
    {
//	sigc::mem_fun(*this, &A::foo)(99);
	sigc::mem_fun(*this,
 		      boost::bind(&A::foo, boost::ref(*this), 99))();

The line above doesn't make sense: the call to sigc::mem_fun() is superfluous.
What you want is:

 sigc::bind(sigc::mem_fun(&A::foo, *this), 99)();

or:

 sigc::bind(&A::foo, sigc::ref(*this), 99)();

Note that mixing boost::bind() (and similar stuff) with the sigc API is possible but not recommended because auto-disconnection from signals that you connect to the resulting functors won't work.

Btw to enable auto-disconnection in your example above, 'A' needs to inherit from sigc::trackable:

 struct A : public sigc::trackable
 {
   [...]
 };

 [...]

 some_signal.connect(sigc::mem_fun(&A::foo, A_object));
 // calling some_signal.emit() after A_object got out-of-scope is safe

 [...]

    }

    void foo(int i){}
};

int main()
{
    A a;
}

Regards,

 Martin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]