I have the following situation, (better in code)
class Foo
{
private:
typedef boost::signal<void ()> Signal;
Signal signal;
public:
void Register_SignalFunction(const Signal::slot_type& slot);
void Unregister_SignalFunction(const Signal::slog_type& slot);
}
class OtherFoo
{
Foo foo;
public:
OtherFoo()
{
foo.Register_SignalFunction(&OnSignal) //I know I can't do this that is precisely my question.
}
void OnSignal(); //what to do when signal fires
}
So the question is, how i pass a 'member-function' pointer to the Register method. Also, is this ok? What I want/need, is some sort of delegate registration system, so if anynone could point my in the right direction I'll appreciate it. Thanx in advance.
Typically you'll either do:
Or, as an inline function:
The latter may be slightly more efficient by removing the layer of indirection
boost::function
has - but only assumingboost::signal
doesn't useboost::function
internally (which it is likely to). So use whichever one you prefer, really.In case anyone wants a full example:
You would typically use boost bind:
What's going on here? :-)
The connect method of the signal takes a functor. That is an object that implements the () operator. bind takes function pointers (either to free functions or member functions) and returns a functor with the right signature.
Also see here:
Complete example using Boost::Signals for C++ Eventing
and here:
how boost::function and boost::bind work
To disconnect a signal store the return value from connect into a:
And then call the disconnect method on that.
I got it working after trying a lot, here's the code:
well this code works as it should be, with one exception, if I unccomment the deviceLost.disconnect(handler) statement, I receive compilation errors like: error C266 "boost::operator ==": 4 overloads have similar conversions.
So, why is this happening? Do you know any other way to accomplish what I'm trying?