boost::signals2 slot as a non-static function member?

Go To StackoverFlow.com

5

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?

2012-04-03 22:20
by nijansen


11

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;
};
2012-04-03 23:14
by Guy Sirton
Ads