Using a member function

Suppose you found a more sophisticated alien alerter class on the web, such as this:

class AlienAlerter : public sigc::trackable
{
public:
    AlienAlerter(char const* servername);
    void alert();
private:
    // ...
};

(Handily it derives from sigc::trackable already. This isn't quite so unlikely as you might think; all appropriate bits of the popular gtkmm library do so, for example.)

You could rewrite your code as follows:

int main()
{
    AlienDetector mydetector;
    AlienAlerter  myalerter("localhost");	// added
    mydetector.signal_detected.connect( sigc::mem_fun(myalerter, &AlienAlerter::alert) ); // changed

    mydetector.run();

    return 0;
}

Note that only 2 lines are different - one to create an instance of the class, and the line to connect the method to the signal.

This code is in example2.cc, which can be compiled in the same way as example1.cc

It's possible to use a lambda expression instead of sigc::mem_fun(), but it's not recommended, if the class derives from sigc::trackable. With a lambda expression you would lose the automatic disconnection that the combination of sigc::trackable and sigc::mem_fun() offers.