The terminology for signals and slots has come under some criticism for not being as intuitive as it maybe could have been, but with a little experience it won't cause a problem. Honest.
So to get some experience, lets look at a simple example...
Lets say you and I are writing an application which informs the user when aliens land in the car park. To keep the design nice and clean, and allow for maximum portability to different interfaces, we decide to use LibSigC++ to split the project in two parts.
I will write the AlienDetector
class, and you will write the code to inform
the user. (Well, OK, I'll write both, but we're pretending, remember?)
Here's my class:
class AlienDetector { public: AlienDetector(); void run(); SigC::Signal0<void> detected; };
(I'll explain the type of detected later.)
Here's your code that uses it:
void warn_people() { cout << "There are aliens in the carpark!" << endl; } int main() { AlienDetector mydetector; mydetector.detected.connect( SigC::slot(warn_people) ); mydetector.run(); return 0; }
Pretty simple really - you call the connect()
method on the signal to
connect your function. connect()
takes a slot
parameter (remember slots
are capable of holding any type of callback), so you convert your
warn_people()
function to a slot using the slot()
function.
To compile this example from the downloadable example code, use:
g++ example1.cc -o eg1 `pkg-config --cflags --libs sigc++-1.2`
Note that those `` characters are backticks, not single quotes. Run it with
./eg1
(Try not to panic when the aliens land!)