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::Signal1<void, std::string> 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 'SignalN
', where N is the number of arguments that
the slots should take. The template parameters are the return type, then the
argument types.
Obviously LibSigC++ doesn't define an infinite number of Signal
templates,
but it does define Signal0
-Signal5
, which should be enough for most people. (If
you know M4, you can tweak it to provide more if you really need to.)
The types in the function signature are in the same order as the template parameters, eg:
SigC::Signal1<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) { cout << "There are aliens in " << where << "!" << endl; } int main() { AlienDetector mydetector; mydetector.detected.connect( SigC::slot(warn_people) ); mydetector.run(); return 0; }
Easy.