c++ - How does libsigc++ convert a function into a slot? -
i'm working basic signal code, , can't figure out how function passed in argument. came across code in libsigc++ tutorial:
aliendetector mydetector; mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
aliendetector has member variable:
sigc::signal<void> signal_detected;
and warn_people function simple print statement:
void warn_people() { printf("they're here!\n"); }
is there example in standard library of using function this? how compiler understand "look for" function being passed in?
it use function pointer. wikipedia has sample code in c: https://en.wikipedia.org/wiki/function_pointer#example_in_c
function pointers useful when actual function call might not known until runtime. instance:
typedef void (*callback_func)(int); callback_func foo_func; void set_foo_func(callback_func callback) { foo_func = callback; } void call_foo_func() { foo_func(5); } void my_func(int a) { ... } int main() { set_foo_func(my_func); call_foo_func(); return exit_success; }
dereferencing optional (or meaningless, depending on how think it) , prefer it:
void call_foo_func() { (*foo_func)(5); } set_foo_func(&my_func);
that's static function. it's little more complicated member functions. , libsigc++ has lots of complicated code lots of useful things.
Comments
Post a Comment