天天看点

C++:17---函数指针

一、格式

  • 指针名前*号,并且将*和指针名用括号括起来

例如:

  1. //指针名为pf,指向一个返回值为bool,参数为两个const string&的函数
  2. bool (*pf)(const string&, const string&);
  3. //这个不是函数指针,而是一个返回值为bool*的pf函数
  4. bool *pf(const string&, const string&);

二、函数指针的赋值

  • 可以直接将函数名赋值给函数指针,或者在函数名前加&符号都可以
  1. bool lengthCompare(const string&, const string&);
  2. bool (*pf)(const string&, const string&);
  3. int main()
  4. {
  5. pf = lengthCompare; //正确
  6. pf = &lengthCompare;//正确
  7. return 0;
  8. }

三、通过函数指针调用函数

调用方式

  • 如果pf是一个函数指针,那么*pf就是该指针所指向的函数,所以(*pf)()就是调用该函数
  • ANSI C标准允许程序员将(*pf)()简写为pf(),但是一定要记住这种写法是一种简写形式

演示案例 

  1. bool lengthCompare(const string&, const string&);
  2. bool (*pf)(const string&, const string&);
  3. int main()
  4. {
  5. pf = lengthCompare;
  6. bool b1 = (*pf)("hello", "goodbye");//相当于在调用lengthCompare("hello","goodbye")
  7. bool b2 = pf("hello","goodbye"); //同上,但是注意这是简写形式
  8. return 0;
  9. }

四、函数指针的注意事项

  • 函数指针可以赋值为空或者0
  • 函数指针必须指向与指针返回值类型以及参数相同的函数

五、重载函数的指针

  • 定义一个重载函数指针时,必须有相对应匹配的重载函数与指针匹配
  1. void ff(int*);
  2. void ff(unsigned int);
  3. void (*pf)(int*)=ff; //正确,指向void ff(int*);
  4. void (*pf2)(int)=ff; //错误,没有匹配的函数
  5. double (*pf3)(unsigned in)=ff; //错误,没有匹配的函数

六、函数/函数指针传参

  • 一个函数的形参为函数或者函数指针时,调用这个函数时,函数形参可以为函数本身也可以为函数指针(调用时本质上传入的都是函数的指针)
  1. bool lengthCompare(const string&, const string&);
  2. void useBigger(const string&, const string&, bool pf(const string&, const string&));
  3. void useBigger2(const string&, const string&, bool (*pf)(const string&, const string&));
  4. int main()
  5. {
  6. bool (*pf)(const string&, const string&);
  7. pf = lengthCompare;
  8. useBigger("hello", "goodbye",lengthCompare);
  9. useBigger("hello", "goodbye", pf);
  10. useBigger2("hello", "goodbye", lengthCompare);
  11. useBigger2("hello", "goodbye", pf);
  12. return 0;
  13. }

七、函数与函数指针取别名

  • 可以使用typedef和decltype来简化函数或函数指针的声明
  • 注意:decltype返回的是函数类型,而不是指针类型,所以Funcp2前面还需要加上*
bool lengthCompare(const string&, const string&);


//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;


void useBigger(const string&, const string&, Func);


void useBigger2(const string&, const string&, FuncP);


int main()
{


bool (*pf)(const string&, const string&);


pf = lengthCompare;


  


useBigger("hello", "goodbye",lengthCompare);


useBigger("hello", "goodbye", pf);


useBigger2("hello", "goodbye", lengthCompare);


useBigger2("hello", "goodbye", pf);


return 0;


}      

八、返回函数的指针

  • 函数和数组一样不能直接通过返回值返回,而需要通过指针返回

格式一:

  • 下面是原始的定义方法,比较繁琐
  • 意义为:f1函数的参数为int,返回值为函数指针类型,该函数指针指向的函数返回值为int,参数为int*和int
int (*f1(int))(int*, int);

格式二:

  • 通过using声明简化
  1. using F = int(int*, int); //F为函数类型
  2. using PF = int(*)(int*, int);//PF为函数指针类型
  3. PF f1(int); //f1函数返回一个函数指针,该指针指向的函数返回值为int,参数为int*和int
  4. F* f1(int); //同上
  5. F f1(int); //错误,F为函数类型,不能直接返回

格式三:

  • 通过C++11标准的尾置返回类型的方式定义
auto f1(int)->int(*)(int*, int);

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

  • 将我们知道一个函数的返回值为某种函数指针类型时,可以使用decltype简化书写

演示案例

  • 例如下面的getFcn函数返回值为sumLength函数指针类型,则可以使用下面的方法
  • 注意:decltype返回的是函数类型,所以还需要加上指针
  1. string::size_type sumLength(const string&, const string&);
  2. string::size_type largerLength(const string&, const string&);
  3. decltype(sumLength) *getFcn();