Functions taking no parameters and returning void are quite useful, especially when they're members of classes that can store unlimited amounts of safely typed data, but they're not sufficient for everything.
What if aliens don't land in the carpark, but somewhere else? Let's modify
the example so that the callback function takes a std::string
with the location
in which aliens were detected.
I change my class to:
class AlienDetector { public: AlienDetector(); void run(); sigc::signal<void(std::string)> signal_detected; // changed };
The only line I had to change was the signal line (in run()
I need to change
my code to supply the argument when I emit the signal too, but that's not shown
here).
The name of the type is 'sigc::signal
'.
The template parameters are the return type, then the argument types in parentheses.
(The parentheses are necessary in libsigc++3. libsigc++2 also accepts a different
syntax, with a comma between the return type and the parameter types. That syntax is
deprecated, though.)
The types in the function signature are in the same order as the template parameters, eg:
sigc::signal<void(std::string)> void function(std::string foo);
So now you can update your alerter (for simplicity, lets go back to the free-standing function version):
void warn_people(std::string where) { std::cout << "There are aliens in " << where << "!" << std::endl; } int main() { AlienDetector mydetector; mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) ); mydetector.run(); return 0; }
Easy.