函数指针(function pointer)

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:

// pf points to function returning bool that takes two const string references
bool (*pf)(const string &, const string &);

This statement declares pf to be a pointer to a function that takes two const string& parameters and has a return type of bool .  We can make function pointers easier to use by defining a synonym for the pointer type using a typedef

typedef bool (*cmpFcn)(const string &, const string &);

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 ."

Using the function name is equivalent to applying the address-of operator to the function name:

// compares lengths of two strings
bool lengthCompare(const string &, const string &);
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;

A pointer to a function can be used to call the function to which it refers. We can use the pointer directly there 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

A function parameter can be a pointer to function. We can write such a parameter in one of two ways:

/* 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 to
function
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 &));

A function can return a pointer to function

// 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);

The best way to read function pointer declarations is from the inside out, starting with the name being declared. We can define a parameter as a function type. A function return type must be a pointer to function; it cannot be a function.

An argument to a parameter that has a function type is automatically converted to the corresponding pointer to function type. The same conversion does not happen when returning a 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

 



posted on 2014-04-27 16:23  江在路上2  阅读(158)  评论(0)    收藏  举报