C++函数指针详解

作者:途径北海道 时间:2021-10-07 15:49:44 

函数指针基础:

1. 获取函数的地址

2. 声明一个函数指针

3.使用函数指针来调用函数

获取函数指针:

函数的地址就是函数名,要将函数作为参数进行传递,必须传递函数名。

声明函数指针

声明指针时,必须指定指针指向的数据类型,同样,声明指向函数的指针时,必须指定指针指向的函数类型,这意味着声明应当指定函数的返回类型以及函数的参数列表。

例如:


double cal(int);   // prototype
double (*pf)(int);   // 指针pf指向的函数, 输入参数为int,返回值为double
pf = cal;    // 指针赋值

如果将指针作为函数的参数传递:


void estimate(int lines, double (*pf)(int));  // 函数指针作为参数传递

使用指针调用函数


double y = cal(5);   // 通过函数调用
double y = (*pf)(5);   // 通过指针调用 推荐的写法
double y = pf(5);     // 这样也对, 但是不推荐这样写

函数指针的使用:


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

double cal_m1(int lines)
{
return 0.05 * lines;
}

double cal_m2(int lines)
{
return 0.5 * lines;
}

void estimate(int line_num, double (*pf)(int lines))
{
cout << "The " << line_num << " need time is: " << (*pf)(line_num) << endl;
}

int main(int argc, char *argv[])
{
int line_num = 10;
// 函数名就是指针,直接传入函数名
estimate(line_num, cal_m1);
estimate(line_num, cal_m2);
return 0;
}

函数指针数组:

这部分非常有意思:


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

// prototype   实质上三个函数的参数列表是等价的
const double* f1(const double arr[], int n);
const double* f2(const double [], int);
const double* f3(const double* , int);

int main(int argc, char *argv[])
{
double a[3] = {12.1, 3.4, 4.5};

// 声明指针
const double* (*p1)(const double*, int) = f1;
cout << "Pointer 1 : " << p1(a, 3) << " : " << *(p1(a, 3)) << endl;
cout << "Pointer 1 : " << (*p1)(a, 3) << " : " << *((*p1)(a, 3)) << endl;

const double* (*parray[3])(const double *, int) = {f1, f2, f3};   // 声明一个指针数组,存储三个函数的地址
cout << "Pointer array : " << parray[2](a, 3) << " : " << *(parray[2](a, 3)) << endl;
cout << "Pointer array : " << parray[2](a, 3) << " : " << *(parray[2](a, 3)) << endl;
   cout << "Pointer array : " << (*parray[2])(a, 3) << " : " << *((*parray[2])(a, 3)) << endl;

return 0;
}

const double* f1(const double arr[], int n)
{
return arr;     // 首地址
}

const double* f2(const double arr[], int n)
{
return arr+1;
}

const double* f3(const double* arr, int n)
{
return arr+2;
}

这里可以只用typedef来减少输入量:


typedef const double* (*pf)(const double [], int);  // 将pf定义为一个类型名称;
pf p1 = f1;
pf p2 = f2;
pf p3 = f3;

来源:https://blog.csdn.net/zj1131190425/article/details/92065897

标签:C++,函数,指针
0
投稿

猜你喜欢

  • Springmvc调用存储过程,并返回存储过程返还的数据方式

    2022-07-24 20:10:44
  • Springboot整合FreeMarker的实现示例

    2023-04-09 00:57:57
  • 将Qt项目升级到Qt6吐血经验总结

    2023-08-22 08:08:30
  • Java的数据类型和参数传递(详解)

    2022-12-30 18:52:25
  • SpringBoot教程_创建第一个SpringBoot项目

    2022-02-19 23:12:54
  • java中的实体类时间格式化

    2022-05-18 02:18:19
  • Mybatis generator自动生成代码插件实例解析

    2022-06-04 22:52:33
  • Java处理Webp图片格式转换的示例代码

    2022-03-09 06:27:29
  • Android 简单实现倒计时功能

    2023-06-27 11:35:40
  • MyBatis根据条件批量修改字段的方式

    2023-12-16 03:24:01
  • java数学工具类Math详解(round方法)

    2022-09-10 17:01:00
  • Android自定义View画圆功能

    2023-05-18 10:47:24
  • Android Studio 新建项目通过git上传到码云图文教程详解

    2021-08-09 12:12:05
  • java实现微信扫码登录第三方网站功能(原理和代码)

    2023-08-04 00:44:11
  • AndroidStudio4.0 New Class的坑(小结)

    2022-07-29 15:06:48
  • Android封装的http请求实用工具类

    2021-09-16 03:03:52
  • 简单实现Android本地音乐播放器

    2021-09-04 19:28:28
  • spring-boot-maven-plugin 配置有啥用

    2022-08-27 19:09:25
  • Java设计模式之访问者模式使用场景及代码示例

    2021-06-27 13:34:25
  • android桌面悬浮窗显示录屏时间控制效果

    2022-04-21 14:54:12
  • asp之家 软件编程 m.aspxhome.com