I have been playing around with boost::signals2
for learning purposes lately, and I was wondering whether I could connect signals to a non-static slot located within a class (like I could in Qt). Consider the following:
class Worker {
typedef boost::signals2::signal<void (const std::string &)> SendMessage;
public:
typedef SendMessage::slot_type SendMessageSlotType;
boost::signals2::connection connect(const SendMessageSlotType &slot) {
return send_message.connect(slot);
}
private:
SendMessage send_message;
};
class Controller {
public:
Controller() {
worker.connect(&Controller::print);
}
private:
static void print(const std::string &message) {
std::cout << message << std::endl;
}
Worker worker;
};
Now I would want to make Controller::print
a non-static member. With boost::thread
for example, this can be achieved using boost::bind
; is there any way to do this with boost::signals2
?
Just:
class Controller {
public:
Controller() {
worker.connect(boost::bind(&Controller::print, this, _1));
}
private:
void print(const std::string &message) {
std::cout << message << std::endl;
}
Worker worker;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With