I'm working with some basic signal code, and I can't figure out how this function is passed in as an argument. I came across this code in the libsigc++ tutorial:
AlienDetector mydetector;
mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
AlienDetector has this member variable:
sigc::signal<void> signal_detected;
And warn_people is a function with a simple print statement:
void warn_people() { printf("They're here!\n"); }
Is there an example in the standard library of using a function like this? How does the compiler understand to "look for" the function that is being passed in?
It use a function pointer. Wikipedia has some sample code in C: https://en.wikipedia.org/wiki/Function_pointer#Example_in_C
Function pointers are useful when the actual function to call might not be known until runtime. For instance:
Dereferencing is optional (or meaningless, depending on how you think about it) and I personally prefer it:
That's just for a static function. It's a little more complicated for member functions. And libsigc++ has lots of very complicated code to do lots of useful things.