views:

37

answers:

1

I have a class Yarl in my code with a member function refresh that I want to bind to two boost::signals. One of these signals is a member of a class EventHandler defined like this:

class EventHandler {
    public:
        boost::signal<void()> sigRefresh;
};

The other is a free floating signal in another file declared like this:

namespace utility {
    static boost::signal<void()> signal_refresh;
}

in a member function of Yarl, I connect refresh to the signals like this:

    events::EventHandler eventHandler;

    eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
    utility::signal_refresh.connect(boost::bind(&Yarl::refresh, this));

and later on I call both signals like this:

sigRefresh();
signal_refresh();

This code compiles and runs, and sigRefresh works exactly as expected. However, nothing happens when I call signal_refresh. As far as I can tell, refresh never actually got connected to signal_refresh. Anyone see what I'm doing wrong?

+1  A: 

I'm taking a guess that you are multiply defining signal_refresh. The static keyword before it's declaration suggests to me the code snippet is in a header file and you had to put the static there to get it to compile without redefined symbol errors. If you have done this then every source file including the header will get a unique copy of signal_refresh and thus the instance you are calling may not be the instance you connected it to.

I may be totally off mark here but it is possible.

bradgonesurfing
That's exactly what I did. I figured it was bad practice, but this was more of a quick hack to see if I could get something to work. Would that cause the signal to fail?
Max
No the signal is not failing. You just need to understand what static means in this context. declaring a non class bound variable as static makes it local to the translation unit ( object file ) and is not exported. You can then have a variable of the same name in each object file without getting a link error. You need to define the signal as extern in the header and create it *without* the static keyword in a single source file. Then you should be going.
bradgonesurfing
It worked. Thanks for your help.
Max