// demofp4.cpp - demonstrate more function pointer communication // using global variables to pass data #include ////////////////////////////////////////////////////////// // 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 std::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() { std::cout << "fun2() using global structure\n" << " ms.x = " << ms.x << "\n" << " ms.y = " << ms.y << "\n"; } void main() { std::cout << "\n\n Demonstrate callback communication\n\n"; funPtr callback; // declare a function pointer callback = (funPtr)fun2; fun1(callback); // pass the function pointer, argument // cast is necessary std::cout << "\n"; }