理解指向函数的指针

关于指向函数的指针的理解
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、指向重载函数的指针
指向重载函数的指针必须与一个重载函数的版本精确匹配,否则,初始化或者赋值后会造成编译错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值