1. 函数指针
函数:bool lengthCompare(const string&, const string&);
函数指针:bool (*pf)(const string&, const string&); //没有初始化
这里首先pf是一个指针,右侧是形参列表,表示pf指向的是函数,再看左侧,发现函数的返回类型是布尔值,因此pf是一个指向函数的指针,函数的参数是两个const string的引用,返回值是bool类型。
注意:*pf两端的括号不能少。如果不写,则表示pf是一个返回值为bool指针的函数
初始化:
pf = lengthCompare;
pf = &lengthCompare; //二者等价,取地址符是可选的
2. 重载函数的指针
必须与重载函数中的某一个精确匹配
如
void ff(int*);
viod ff(unsigned int);
void (*pf1)(unsigned int) = ff;//pf1指向ff(unsigned)
void (*pf2)(int) = ff;//error
double (*pf2)(int*) = ff; //error
3. 函数指针形参
void useBigger(const string& s1, const string& s2, bool pf(const string&, const string&));
等价于
void useBigger(const string& s1, const string& s2, bool (*pf)(const string&, const string&));
实例化:useBigger(s1, s2, lengthCompare);