7.9. Pointers to Functions
A function pointer is just thata pointer that denotes a function rather than an object. Like any other pointer, a function pointer points to a particular type. A function's type is determined by its return type and its parameter list. A function's name is not part of its type:
This statement declares pf to be a pointer to a function that takes two const string& parameters and has a return type of bool.
// pf points to function returning bool that takes two const string references
bool (*pf)(const string &, const string &);

// declares a function named pf that returns a bool*
bool *pf(const string &, const string &);
Using Typedefs to Simplify Function Pointer Definitions
Function pointer types can quickly become unwieldy. We can make function pointers easier to use by defining a synonym for the pointer type using a typedef (Section 2.6, p. 61):
This definition says that cmpFcn is the name of a type that is a pointer to function. That pointer has the type "pointer to a function that returns a bool and takes two references to const string." When we need to use this function pointer type, we can do so by using cmpFcn, rather than having to write the full type definition each time.
typedef bool (*cmpFcn)(const string &, const string &);
Initializing and Assigning Pointers to Functions
When we use a function name without calling it, the name is automatically treated as a pointer to a function. Given
any use of lengthCompare, except as the left-hand operand of a function call, is treated as a pointer whose type is
// compares lengths of two strings
bool lengthCompare(const string &, const string &);
We can use a function name to initialize or assign to a function pointer:
bool (*)(const string &, const string &);
Using the function name is equivalent to applying the address-of operator to the function name:
cmpFcn pf1 = 0; // ok: unbound pointer to function
cmpFcn pf2 = lengthCompare; // ok: pointer type matches function's type
pf1 = lengthCompare; // ok: pointer type matches function's type
pf2 = pf1; // ok: pointer types match
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;

string::size_type sumLength(const string&, const string&);
bool cstringCompare(char*, char*);
// pointer to function returning bool taking two const string&
cmpFcn pf;
pf = sumLength; // error: return type differs
pf = cstringCompare; // error: parameter types differ
pf = lengthCompare; // ok: function and pointer types match exactly
Calling a Function through a Pointer
A pointer to a function can be used to call the function to which it refers. We can use the pointer directlythere is no need to use the dereference operator to call the function
cmpFcn pf = lengthCompare;
lengthCompare("hi", "bye"); // direct call
pf("hi", "bye"); // equivalent call: pf1 implicitly dereferenced
(*pf)("hi", "bye"); // equivalent call: pf1 explicitly dereferenced

Function Pointer Parameters
A function parameter can be a pointer to function. We can write such a parameter in one of two ways:
[View full width]/* useBigger function's third parameter is a pointer to function
* that function returns a bool and takes two const string references
* two ways to specify that parameter:
*/
// third parameter is a function type and is automatically treated as a pointer tofunction
void useBigger(const string &, const string &,
bool(const string &, const string &));
// equivalent declaration: explicitly define the parameter as a pointer to function
void useBigger(const string &, const string &,
bool (*)(const string &, const string &));
Returning a Pointer to Function
A function can return a pointer to function, although correctly writing the return type can be a challenge:
// ff is a function taking an int and returning a function pointer
// the function pointed to returns an int and takes an int* and an int
int (*ff(int))(int*, int);

says that ff is a function taking one parameter of type int. This function returns
ff(int)
a pointer to a function that returns an int and takes two parameters of type int* and an int.Typedefs can make such declarations considerably easier to read:
int (*)(int*, int);
// PF is a pointer to a function returning an int, taking an int* and an int
typedef int (*PF)(int*, int);
PF ff(int); // ff returns a pointer to function

// func is a function type, not a pointer to function!
typedef int func(int*, int);
void f1(func); // ok: f1 has a parameter of function type
func f2(int); // error: f2 has a return type of function type
func *f3(int); // ok: f3 returns a pointer to function type
Pointers to Overloaded Functions
It is possible to use a function pointer to refer to an overloaded function:
The type of the pointer and one of the overloaded functions must match exactly. If no function matches exactly, the initialization or assignment results in a compile-time error:
extern void ff(vector<double>);
extern void ff(unsigned int);
// which function does pf1 refer to?
void (*pf1)(unsigned int) = &ff; // ff(unsigned)
// error: no match: invalid parameter list
void (*pf2)(int) = &ff;
// error: no match: invalid return type
double (*pf3)(vector<double>);
pf3 = &ff;