Using Bind with Signals (version 2)
Similar to my last post, this is a slightly different implementation
where the handler has a signal of its own. Now I can't seem to bind to
the handler functor, getting a compiler message like:
/usr/include/boost/bind.hpp:60: error: `bool (handler::*)(char)' is not
a class, struct, or union type
The goal is that when the keypress signal is emitted, it will execute
the handler which will receive the char (key), test it, and emit its
signal (sig) if appropriate.
Any help would again be much appreciated, the short code follow, with
the problematic bind statement commented:
Thanks in advance,
Darren Hart
compile: g++ test.cpp -o test -lboost_signals
// test.cpp ----------------------------------------------------
#include <iostream>
#include
Darren Vincent Hart wrote: [...]
struct handler { char key_; boost::signal
sig; handler(char key) : key_(key) { }
bool operator()(char key) { cout << "handler::operator() - received a " << key << endl; if (!sig.empty() && key == key_) { cout << "\texecuting slot\n\t"; sig(); } cout << endl; return true; } };
[...]
// THE PROBLEM IS HERE - HOW DO I BIND TO handler_a.test(char) ? widget_b.keypress.connect( boost::bind(&handler::operator(), &handler_a) ); // END PROBLEM
I think that you want widget_b.keypress.connect( boost::ref(handler_a) ); The reason for the error is that &handler::operator() takes two arguments (the implicit 'this' and 'char key') but you are trying to bind it to just one, &handler_a. The correct statement is widget_b.keypress.connect( boost::bind(&handler::operator(), &handler_a, _1) );
participants (2)
-
Darren Vincent Hart
-
Peter Dimov