关于指向函数的指针的理解
1、bool (*pf)(const &string const &string)//这条语句将pf声明为指向一个函数类型的指针,就像是指向一个int类型的声明一样的。
该函数类型定义为,带有两个const &string形参并返回一个bool型的变量。
*pf两侧的括号是必须要加上的,不能遗忘。否则就会发生完全不同的两回事情,哈哈。
bool *pf(const &string const &string)//他声明一个名字为pf的函数,返回一个bool型的指针变量
2、使用typedef简化函数指针的定义
typedef bool (*cmpFcn)(const string &, const string &);
cmpFcn f1;//f1是一个指向函数的指针
3、函数指针的初始化
bool lengthCompare(const string &, const string &);
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;
看到没,使用取地址符和不使用取地址符是一样的ok
4、使用指针调用函数
cmpFcn pf = lengthCompare;
lengthCompare("hi", "bye"); // direct call
pf("hi", "bye"); // equivalent call: pf1 implicitly dereferenced
(*pf)("hi", "bye"); // equivalent call: pf1 explicitly dereferenced
看到没,使用解引用和不适用接引用都是一样的ok
5、使用函数指针作为形参的方法
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 &));
6、理解函数返回指向函数的指针变量
int (*ff(int))(int*, int); //他声明了一个函数ff(int),并且该函数返回一个指向int (*)(int *,int)类型函数的指针。
@看起来很麻烦是不是,可以使用typedef进行简化
typedef int (*pf)(int *,int);
pf ff(int)//是不是更容易理解,嘻嘻
7、指向重载函数的指针
指向重载函数的指针必须与一个重载函数的版本精确匹配,否则,初始化或者赋值后会造成编译错误。
1、bool (*pf)(const &string const &string)//这条语句将pf声明为指向一个函数类型的指针,就像是指向一个int类型的声明一样的。
该函数类型定义为,带有两个const &string形参并返回一个bool型的变量。
*pf两侧的括号是必须要加上的,不能遗忘。否则就会发生完全不同的两回事情,哈哈。
bool *pf(const &string const &string)//他声明一个名字为pf的函数,返回一个bool型的指针变量
2、使用typedef简化函数指针的定义
typedef bool (*cmpFcn)(const string &, const string &);
cmpFcn f1;//f1是一个指向函数的指针
3、函数指针的初始化
bool lengthCompare(const string &, const string &);
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;
看到没,使用取地址符和不使用取地址符是一样的ok
4、使用指针调用函数
cmpFcn pf = lengthCompare;
lengthCompare("hi", "bye"); // direct call
pf("hi", "bye"); // equivalent call: pf1 implicitly dereferenced
(*pf)("hi", "bye"); // equivalent call: pf1 explicitly dereferenced
看到没,使用解引用和不适用接引用都是一样的ok
5、使用函数指针作为形参的方法
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 &));
6、理解函数返回指向函数的指针变量
int (*ff(int))(int*, int); //他声明了一个函数ff(int),并且该函数返回一个指向int (*)(int *,int)类型函数的指针。
@看起来很麻烦是不是,可以使用typedef进行简化
typedef int (*pf)(int *,int);
pf ff(int)//是不是更容易理解,嘻嘻
7、指向重载函数的指针
指向重载函数的指针必须与一个重载函数的版本精确匹配,否则,初始化或者赋值后会造成编译错误。