Tuesday, September 30, 2008

How to Pass a Function Pointer in C++

Sample C++ code snippet...


void PassPtr(int (*ptr2Func)(float, char, char))
{
float result = ptr2Func(12, 'a', 'b'); // call using function pointer

cout << result << endl;
} /* is a pointer to a function which returns an int and takes a float and two char */

void Pass_A_Function_Pointer()
{
cout << endl << "Executing 'Pass_A_Function_Pointer'" << endl;
PassPtr(&DoIt);
} // execute example code - 'DoIt' is a suitable function

0 comments: