C++函数指针

来源:互联网 发布:sql 修改字段内容 编辑:程序博客网 时间:2024/06/06 11:10

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

bool lengthCompare(const string &, const string &);

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

bool (*pf)(const string &, const string &);  //未初始化

使用函数指针

可以把函数名作为一个值使用,该函数自动地转换成指针。例如,按照如下形式可以将lengthCompare的地址赋给pf:

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

还可以直接使用指向函数的指针调用该函数,无需提前解引用指针:

bool b1 = pf("hello","goodbye");    //调用lengthCompare函数bool b2 = (*pf)("hello","goodbye"); //一个等价的调用bool b3 = lengthCompare("hello","goodbye"); //另一个等价的调用

在指向不同函数类型的指针间不存在转换规则。但是可以为函数指针赋一个nullptr或者值为0整型表达式,表示该指针没有指向任何一个函数:

string::size_type sumLength(const string &, const string &);bool cstringCompare(const char*, const char*);pf = 0; //正确:pf不指向任何函数pf = sumLength; //错误,返回类型不匹配pf = cstringCompare;    //错误:形参类型不匹配pf = lenghCompare;  //正确:函数和指针的类型精确匹配

重载函数的指针
当使用重载函数时,上下文必须清晰地界定到底应该选用哪个函数。如果定义了指向重载函数的指针

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

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

void (*pf2)(int) = ff;  //错误:没有任何一个ff与该形参列表匹配double (*pf3)(int*) = ff;   //错误:ff和pf3的返回类型不匹配

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

//第三个形参是函数类型,它会自动地转换成指向函数的指针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 &));

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

//自动将函数lengthCompare转换成指向该函数的指针useBigger(s1,s2,lengthCompare);

直接使用函数指针类型显得冗长而繁琐。类型别名和decltype能简化使用函数指针的代码:

//Func 和 Func2 是函数类型typedef bool Func(const string &, const string &);typedef decltype(lengthCompare) Func2;  //等价的类型//Func 和 Func2 是指向函数的指针typedef bool (*Func)(const string &, const string &);typedef decltype(lengthCompare) Func2 *FuncP2;  //等价的类型

这里需要注意的是decltype返回函数类型,此时不会将函数类型自动转换成指针类型。需要注意的是,decltype返回函数类型,所以只有在结果前面加上*才能得到指针。
可以使用如下的形式重新声明useBigger:

//useBigger的等价声明,其中使用了类型别名void useBigger(const string &, const string &, Func);void useBigger(const string &, const string &, Func);

这两个声明语句声明的是同一个函数,在第一条语句中,编译器自动地将Func表示的函数类型转换成指针。

返回指向函数的指针
虽然不能返回 一个函数,但是能返回指向函数类型的指针。必须把返回类型写成指针的形式,编译器不会自动地将函数返回类型当成对应的指针类型处理。最简单的办法是使用类型别名:

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

和函数类型的形参不一样,返回类型不会自动地转换成指针。必须显示地将返回类型指定为指针:

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

当然,也可以直接声明f1:

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

按照由内向外的顺序阅读这条声明语句:f1有形参列表,所以f1是个函数;f1前面有*,所以f1返回一个指针,进一步观察发现,指针的类型本身也包含形参列表,因此指针指向函数,该函数的返回类型是int。
还可以使用尾置返回类型的方式声明一个返回函数指针的函数:

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

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

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

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

0 0
原创粉丝点击