| 37 | // ---------------------------------------------------------------------------------------- |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | // create a pointer that can point to member functions that take no arguments |
| 42 | member_function_pointer<> mfp1; |
| 43 | |
| 44 | // create a pointer that can point to member functions that take a single int argument |
| 45 | member_function_pointer<int> mfp2; |
| 46 | |
| 47 | example_object obj; |
| 48 | |
| 49 | // now we set the mfp1 pointer to point to the member function do_something() |
| 50 | // on the obj object. |
| 51 | mfp1.set(obj, &example_object::do_something); |
| 52 | |
| 53 | |
| 54 | // now we set the mfp1 pointer to point to the member function print_this_number() |
| 55 | // on the obj object. |
| 56 | mfp2.set(obj, &example_object::print_this_number); |
| 57 | |
| 58 | |
| 59 | // Now we can call the function this pointer points to. This calls the function |
| 60 | // obj.do_something() via our member function pointer. |
| 61 | mfp1(); |
| 62 | |
| 63 | // Now we can call the function this pointer points to. This calls the function |
| 64 | // obj.print_this_number(5) via our member function pointer. |
| 65 | mfp2(5); |
| 66 | |
| 67 | |
| 68 | // The above example shows a very simple use of the member_function_pointer. |
| 69 | // A more interesting use of the member_function_pointer is in the implementation |
| 70 | // of callbacks or event handlers. For example, when you register an event |
| 71 | // handler for a dlib::button click it uses a member_function_pointer |
| 72 | // internally to save and later call your event handler. |
| 73 | } |
| 74 | |
| 75 | // ---------------------------------------------------------------------------------------- |
| 76 | |