Boost :: signal2 slot as a non-static member of a function?

Recently, I have been playing with boost::signals2for training, and I was wondering if I can connect signals to a non-static slot located inside the class (for example, 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 like to make a Controller::printnon-stationary member. For example, when boost::threadthis can be achieved with boost::bind; is there any way to do this using boost::signals2?

+5
source share
1 answer

Simply:

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;
};
+11
source

All Articles