// demofp4.cpp - demonstrate more function pointer communication #include using namespace std; ////////////////////////////////////////////////////////// // module using function pointers defines the callback // // signature and the function(s) using callbacks // ////////////////////////////////////////////////////////// // declare a funPtr type taking no argument typedef void(*funPtr)(); void fun1(funPtr fptr) { // this is function using // callback argument cout << " fun1 calling "; fptr(); // invoke the function pointed to } // note that this module doesn't // know what fptr does nor its // input data structure /////////////////////////////////////////////////////////////// // client code defines the callback functions and contents // // of their calling parameter(s) // /////////////////////////////////////////////////////////////// // client supplied callback input data - it's global struct myStruct { int x; // it can hold anything the design needs double y; } ms = { 1, 2.5 }; // client supplied callback function void fun2() { cout << "fun2() using global structure\n" << " ms.x = " << ms.x << "\n" << " ms.y = " << ms.y << "\n"; } int main() { cout << "\n\ndemonstrating callback communication\n\n"; funPtr callback; // declare a function pointer callback = (funPtr)fun2; fun1(callback); // pass the function pointer, argument // cast is necessary cout << "\n\n"; return 0; }