函数指针

函数指针指向的是函数而非对象
函数的类型由它的返回类型和形参类型共同决定。
//比较两个string对象的长度
bool lengthCompare(const string &, const string &);
该函数的类型是bool(const string&, const string&)。想要声明一个指向函数的指针,只需要用指针替换函数名即可:

//pf指向一个函数,该函数的参数是两个const string的引用,返回bool值
bool (*pf)(const string &, const string &); //未被初始化

使用函数指针
pf = lengthCompare;
pf = &lengthCompare;
等价语句,取地址符是可选的。
bool b1 = pf(“hello”, “goodbye”);
bool b2 = (*pf)(“hello”,”goodbye”);
boo b3 = lengthCompare(“hello”,”goodbye”);
三者等价

重载函数的指针
void ff(int*);
void ff(unsigned int);

void (*pf1)(unsigned int) = ff;
编译器通过指针类型巨鼎选用哪个函数。

函数指针形参
与数组类似,不能定义函数类型的形参,但是形参可以使指向函数的指针。此时,形参虽然看起来是函数类型,实际上却是当做指针在用。
//第三个形参是函数类型,它会自动地转换成指向函数的指针
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 &));

类型别名和decltype使得我们能够简化使用函数指针的代码。

//Func和Func2是函数类型
typedef bool Func(const string&, const string&);
typedef decltype(lengthCompare) Func2;

//FuncP和FuncP2是指向函数的指针
typedef bool (*FuncP)(const string&, const string&);
typedef decltype(lengthCompare) *FuncP2;

//useBigger的等价声明,其中使用了类型别名
void useBigger(const string&, const string&, Func);
void useBigger(const string&, const string&, FuncP2);
声明的是同一个函数,在第一条语句中,编译器自动将Func函数类型转换成指针

返回指向函数的指针
与数组指针类似,虽然不能返回一个函数,但是能够返回指向函数类型的指针。然而我们必须把返回类型写成指针类型,编译器不会自动地把函数返回类型当成对应的指针类型处理

声明一个返回函数指针的函数,最简单的办法是使用类型别名。
using F = int(int*,int); //F是函数类型,不是指针
using PF = int()(int, int); //PF是指针类型

我们必须显式的将返回类型指定为指针:
PF f1(int); //正确,PF指向函数的指针,f1返回指向函数的指针
F f2(int); //错误,F为函数类型
F *f2(int); //正确,显式地指定返回类型是指向函数的指针

当然我们也能用下面的形式直接声明f1:
int (f(int))(int, int)
按照由内向外的顺序阅读这条声明语句:

我们看到f1有形参列表,所以f1是个函数;
f1前面有*,所以f返回一个指针;
指针本身也包含形参列表
因此

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值