Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-08-07 11:33:22


On Wednesday 07 August 2002 12:07 pm, Brunen, Johannes wrote:
> Hi,
>
> I have tried to connect a slot function to a signal as follows:
>
> class X
> {
> public:
> void Connect(boost::signal0<XVOID>::slot_function_type f);
> private:
> boost::signal0<XVOID> _signal;
> };
>
> void X::Connect(boost::signal0<XVOID>::slot_function_type f)
> {
> _signal.connect(f);
> }

You'll want to use boost::signal0<XVOID>::slot_type instead of
boost::signal0<XVOID>::slot_function_type. Otherwise, bad things will happen
if you are expecting automatic connection lifetime management to work.

> class A
> {
> void somefun(X& x);
> public:
> void f ();
> };
>
> A::somefun(X& x)
> {
> x.Connect(boost::mem_fn(&A::f));
> }
>
> This does not compile. Isn't it possible to use a simple member function as
> a slot?
> A slot functor works just fine but is a little bit overkill in my
> application.

The problem here is that there is no 'this' argument specified when calling
&A::f, so boost::mem_fn(&A::f) returns a function argument that takes one
parameter (of type A*/A&/shared_ptr<A>/whatever). So you need to bind that
argument to 'this', like so:

  x.Connect(boost::bind(&A::f, this));

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk