概要:

  • 函数指针介绍
  • typedef简化函数指针的定义
  • 指向函数的指针的初始化和赋值
  • 通过指针调用函数
  • 函数指针形参
  • 返回指向函数的指针
  • 指向重载函数的指针

参考《C++ Primer》 第五版


  • 函数指针介绍

函数指针是C++中比较灵活而且重要的部分,对于软件的灵活度上有很大的帮助 !

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

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

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

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

Note: *pf 两端的括号必不可少,如果不写括号,则pf是一个返回值为bool的指针的函数:

//声明一个名为pf的函数,该函数返回bool*
bool *pf(const string &, const string &);

  • typedef简化函数指针的定义

现在我们来定义三个函数指针:

bool(*pf1)(const string &, const string &);
bool(*pf2)(const string &, const string &);
bool(*pf3)(const string &, const string &);

有没有发现一个问题,每次定义都需要这么长,有没有好的办法呢,当然是有的,我们可以用到typedef:

typedef bool(*cmpFcn)(const string &, const string &);
//bool(*pf1)(const string &, const string &);
//bool(*pf2)(const string &, const string &);
//bool(*pf3)(const string &, const string &);
cmpFcn pf1;
cmpFcn pf2;
cmpFcn pf3;

  • 指向函数的指针的初始化和赋值

函数指针的赋值:

pf = lengthCompare ;
pf = &lengthCompare ;

因为在C/C++里函数名就是地址,所以以上两者都等价,都可以给函数指针赋值。


  • 通过指针调用函数

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

bool b1=pf("hello","goodbye");
bool b2=(*pf)("hello","goodbye");
bool b3=LengthCompare("hello","goodbye");
//三个等价调用

指向不同函数类型的指针之间不存在转换规则。

我们可以为函数指针赋一个 nullptr或者0 的整型常量表达式,表示该指针没有指向任何一个函数。

bool lengthCompare(const string &s1, const string &s2) {
return s1.size() == s2.size();
} string::size_type sumLength(const string &s1, const string &s2) {
return s1.size() + s2.size();
}
bool cstringCompare(char *s1, char *s2) {
return strlen(s1) == strlen(s2);
} pf1 = ;
pf2 = lengthCompare;
//pf3 = sumLength;
//pf4 = cstringCompare;

如果函数返回类型不匹配(如上)、或者形参不匹配,也会报错!


  • 函数指针形参

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

#include<iostream>
#include<string>
using namespace std;
typedef bool(*cmpFcn)(const string &, const string &);
//bool(*pf1)(const string &, const string &);
//bool(*pf2)(const string &, const string &);
//bool(*pf3)(const string &, const string &);
bool lengthCompare(const string &s1, const string &s2) {
return s1.size() == s2.size();
}//第三个形参是函数类型,它会自动地转成指向函数类型的指针
void useBigger(const string &s1, const string &s2, bool(*pf)(const string&, const string&)) {
cout << pf(s1, s2) << endl;
} int main()
{
cmpFcn pf = lengthCompare;
useBigger("hi", "func", pf);
system("pause");
return ;
}

  • 返回指向函数的指针

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

#include<iostream>
#include<string>
#include<vector>
using namespace std; int demo(int *p, int a) {
return ;
} // ff是一个函数,有一个形参x,返回结果是一个函数指针int(*)(int *,int)
int (*ff(int x))(int *, int) {
cout << x << endl;
return demo;
} int main()
{
int a = ;
cout << ff()(&a, a)<< endl;
system("pause");
return ;
}

同样,要想声明一个返回函数类型的指针,最简单的方法是使用类型别名:

typedef int (*PF)(int *, int);

PF是一个函数指针,指向的函数有两个形参。

于是就可以这么写了:

// ff是一个函数,有一个形参x,返回结果是一个函数指针int(*)(int *,int)
//int(*ff(int x))(int *, int) {
// cout << x << endl;
// return demo;
//} PF ff(int x) {
cout << x << endl;
return demo;
}

  • 指向重载函数的指针

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

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void ff(vector<double> vec) {
cout << "ff(vector<double vec)" << endl;
} void ff(unsigned int x) {
cout << "ff(unsigned int x)" << endl;
} int main()
{
//void(*pf)(int x) = &ff; 报错
void (*pf)(unsigned int x) = &ff;
//double (*pf2)(vector<double>) = &ff; 报错
void (*pf2)(vector<double>) = &ff;
system("pause");
return ;
}

C/C++中的函数指针的使用与总结的更多相关文章

  1. QT中使用函数指针

    想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...

  2. Delphi中的函数指针判断是否为空

    delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...

  3. C++中使用函数指针 【瓦特芯笔记】

         在C++类中使用函数指针. 类型定义:      typedef 返回类型(类名::*新类型)(参数表) //类定义 class CA { public: char lcFun(int a) ...

  4. 1、C语言中的函数指针

    一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...

  5. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  6. Keil C51 中的函数指针和再入函数

    函数指针是C语言中几个难点之一.由于8051的C编译器的独特要求,函数指针和再入函数有更多的挑战需要克服.主要由于函数变量的传递.典型的(绝大部分8051芯片)函数变量通过堆栈的入栈和出栈命令来传递. ...

  7. C++中的函数指针和指针函数

    C++中的函数指针和指针函数       数组名一般可以被当成指向数组第一个元素的常量指针,同样的情况,在函数中,函数名可以本当成指向函数的常量指针,假如一个函数已经定义,那么它在计算机中一定有特定的 ...

  8. C语言中的函数指针

    C语言中的函数指针 函数指针的概念:   函数指针是一个指向位于代码段的函数代码的指针. 函数指针的使用: #include<stdio.h> typedef struct (*fun_t ...

  9. 利用C语言中的函数指针实现c++中的虚函数

    C语言中的函数指针 #include<stdio.h> int fun1(int a) { return a*a; } int fun2(int a) { return a*a*a; } ...

  10. C/C++中的函数指针

    C/C++中的函数指针 一.引子 今天无聊刷了leetcode上的一道题,如下: Median is the middle value in an ordered integer list. If t ...

随机推荐

  1. SpringBoot中的ajax跨域问题

    在控制类加入注释@CrossOrigin(allowCredentials = "true",allowedHeaders = "*",origins = {& ...

  2. MySQL之UNION与UNION ALL

    数据表中的数据如下: UNION: 可以获取books表与articles表中所有不同的title,如果两个表中title相同的只会显示一个.  UNION ALL : 可以获取books表与arti ...

  3. ssh操作服务器

    # -*- coding: utf-8 -*- """ Created on Wed Mar 20 10:15:16 2019 @author: Kuma 1. ssh连 ...

  4. hexdump

    一个十六进制格式化输出: #include <stdio.h> #include <stdlib.h> #include <string.h> void hexdu ...

  5. ss with kcptun

    install ss apt search shadowsocks shadowsocks/kali-rolling,kali-rolling,now 2.9.0-2 all [installed] ...

  6. eclipse启动tomcat错误解决

    clipse启动tomcat报出下面的错误提示: 控制台: 九月 06, 2018 9:01:31 下午 org.apache.tomcat.util.digester.SetPropertiesRu ...

  7. 64位ubuntu16.04系统安装网易云音乐

    64位ubuntu16.04系统安装网易云音乐 1.官网下载安装包:netease-cloud-music_1.1.0_amd64_ubuntu.deb https://music.163.com/# ...

  8. 前段时间碰到的一些问题(免费WiFi设置+fiddler对手机进行抓包+fiddler抓不到https的请求)

    这段时间转入移动端测试,对这块比较陌生,工作开展起来比较困难,所以好多东西都只是以解决问题为第一目标,没有去细细推敲其中原理,可能会有些语无伦次之感,但还是记一下当时解决问题的大致思路,供以后参考. ...

  9. less--入门

    Less(Learner Style Sheets)是向后兼容css扩展语言. 变量(Variables) @width: 10px; @height: @width + 10px; header{ ...

  10. JavaScript 之存取器属性

    首先介绍一下此篇随笔的主角: Object.getOwnPropertyDescriptor 和 Object.getOwnPropertyDescriptors 通过这两个api,可以访问除 nul ...