C++ 杂记

010 函数指针

2020-03-06  本文已影响0人  赵者也

函数指针指向的是函数而非对象。和其他指针一样,函数指针指向某种特定类型。函数的类型由它的返回类型和形参类型共同决定,与函数名称无关。例如:

bool LengthCompare(const std::string&, const std::string&);

该函数的类型是 bool (const std::string&, const std::string&)。要想声明一个可以指向该函数的指针,只需要用指针替换函数名即可:

bool (*lcPtr)(const std::string&, const std::string&);

注意:*lcPtr 两端的括号必不可少。如果不写这对括号,则 lcPtr 是一个返回值为 bool 指针的函数。

使用函数指针

当我们把函数名作为一个值使用时,该函数自动转换为指针。例如:

lcPtr = LengthCompare;    // lcPtr 指向名为 LengthCompare 的函数
lcPtr = &LengthCompare;   // 等价的赋值语句:取地址符是可选的

此外,我们还能直接使用指向函数的指针调用该函数,无须提前解引用指针:

bool b1 = lcPtr("Hello", "World!");     // 调用 LengthCompare 函数
bool b2 = (*lcPtr)("Hello", "World!");  // 一个等价的调用
bool b3 = LengthCompare("Hello", "World!"); // 另一个等价的调用

在指向不同函数类型的指针间不存在转换规则。但是和往常一样,我们可以为函数指针赋值一个 nullptr,表示该指针没有指向任何一个函数:

bool LengthCompare(const std::string&, const std::string&);
bool (*lcPtr)(const std::string&, const std::string&);
std::string::size_type sumLength(const std::string&, const std::string&);
bool cstringCompare(const char*, const char*);

lcPtr = nullptr;    // 正确:形参类型不匹配
lcPtr = sumLength;  // 错误:返回类型不匹配
lcPtr = cstringCompare; // 错误:形参类型不匹配
lcPtr = LengthCompare;  // 正确:函数和指针的类型精确匹配

重载函数的指针

当我们使用重载函数时,代码必须清晰地指明到底应该使用哪个函数。例如:

void ff(int*);
void ff(unsigned);

编译器通过指针类型决定选用哪个函数,指针类型必须与重载函数中的某一个精确匹配:

void ff(int*);
void ff(unsigned);
void (*pf1)(unsigned) = ff;  // pf1 指向 ff(unsigned)
void (*pf2)(int) = ff;  // 错误
void (*pf3)(int*) = ff;  // 错误

函数指针形参

和数组类似,虽然不能定义函数类型的形参,但是形参可以是指向函数的指针。此时,形参看起来是函数类型,实际上却是当成指针使用:

// 第三个参数是函数类型,它会自动地转换成指向函数的指针
void useBigger(const std::string &s1, const std::string &s2, bool pf(const std::string&, const std::string&));
// 等价的声明:显示地将形参定义成指向函数的指针
void useBigger(const std::string &s1, const std::string &s2, bool (*pf)(const std::string&, const std::string&));

我们可以直接把函数作为实参使用,此时它会自动转换成指针:

useBigger(s1, s2, LengthCompare);

useBigger 的声明语句看起来冗长而繁琐。类型别名和 decltype 能让我们简化使用了函数指针的代码:

// Func1 和 Func2 是函数类型
typedef bool Func1(const std::string&, const std::string&);
typedef decltype (LengthCompare) Func2; // 等价的类型
// FuncP1 和 FuncP2 是指向函数的指针
typedef bool (*FuncP1)(const std::string&, const std::string&);
typedef decltype (LengthCompare) *FuncP2; // 等价的类型

然后,可以使用如下方式改写 useBigger:

void useBigger(const std::string &s1, const std::string &s2, Func1);
void useBigger(const std::string &s1, const std::string &s2, FuncP2);

返回指向函数的指针

与数组类似,虽然不能返回一个函数,但是能返回指向函数类型的指针。然而,我们必须把返回类型写成指针形式,编译器不会自动地将函数返回类型当成对应的指针类型处理。要想声明一个返回函数指针的函数,最简单的方法是借助类型别名:

using F = int(int*, int);    // F 是函数类型,不是指针
using PF = int(*)(int*, int);  // PF 是指针类型

我们必须显式地将返回类型指定为指针:

PF f1(int);    // 正确:PF 是指向函数的指针,f1 返回指向函数的指针
F f2(int);    // 错误:F 是函数类型,f2 不能返回一个函数
F *f3(int);    // 正确

当然,我们也能用下面的形式直接地声明 f1:

int (*f1(int))(int*, int);

另外,我们还可以使用尾置返回类型的方式:

auto f1(int)->int (*)(int*, int);

将 auto 和 decltype 用于函数指针类型

如果我们明确知道返回的函数是哪一个,就能使用 decltype 简化书写函数指针返回类型的过程。例如假定有两个函数,它们的返回类型都是 std::string::size_type,并且各有两个 const std::string& 类型的形参,此时我们可以编写第三个函数,它接受一个 std::string 类型的参数,返回一个指针,该指针指向前两个函数中的一个:

std::string::size_type sumLength(const std::string&, const std::string&);
std::string::size_type largerLength(const std::string&, const std::string&);
// 根据其形参的取值,getFcn 函数返回指向 sumLength 或者 largerLength 的指针
decltype (sumLength) *getFcn(const std::string&);

声明 getFcn 唯一需要注意的地方是,牢记当我们将 decltype 作用于某个函数时,它返回函数类型而非指针类型。因此,我们显示地加上 * 以表明我们需要返回指针,而非函数本身。

上一篇 下一篇

猜你喜欢

热点阅读