【C++】深入理解函数指针

版权声明:本文为博主原创文章,未经允许,不得转载!欢迎留言附带链接转载! https://blog.csdn.net/qq_15698613/article/details/89434824

目录

第1部分:

第1.1 回顾函数定义

第1.2 一个函数指针

1.2.1程序示例 

1.3 函数指针数组 


第1部分:

第1.1 回顾函数定义

首先来看,下面3个表示的特征标和返回类型都是相同的

括号里面:

const double ar[]  与const double *ar 含义完全相同,函数原型中,可以省略 标识符

const double ar[]可以简化为const double []

const double *ar可以简化为const double *

括号外面:函数定义必须提供标识符,因此必须使用const double ar[] 或者  const double * ar

第1.2 一个函数指针

假设要声明一个指针,指向三个函数中的一个,指针名为pa, 则将函数名替换为(*pa)

进一步我们看一下使用:

注意:原型是double * f1

所以这里的输出前一项是double的地址,后一项才是存储的值

*(*p1)(av,3) 和  *p2(av,3)这个前面 的*用来解析地址输出值

1.2.1程序示例 

//arfupt.cpp -- 数组函数指针
#include<iostream>
using namespace std;
// 相同的声明
const double * f1(const double ar[], int n);
const double * f2(const double ar[], int n);
const double * f3(const double ar[], int n);

int main()
{
	double av[3] = { 111.1, 222.2, 333.3 };

	//函数指针,并且直接赋值了
	const double *(*p1)(const double *, int) = f1;
	auto p2 = f2;//使用C++11特性
	//const double *(*p2)(const double *, int) = f2;

	cout << "使用函数指针:\n";
	cout << "地址\n";
	cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;
	cout << p2(av, 3) << ": " << *p2(av, 3) << endl;

	return 0;
}

const double *f1(const double* ar, int n)
{
	return ar;
}
const double *f2(const double ar[], int n)
{
	return ar + 1;
}
const double *f3(const double ar[], int n)
{
	return ar + 2;
}

运行结果:

 

1.3 函数指针数组 

示例:

//arfupt.cpp -- 数组函数指针
#include<iostream>
using namespace std;
// 相同的声明
const double * f1(const double ar[], int n);
const double * f2(const double ar[], int n);
const double * f3(const double ar[], int n);

int main()
{
	double av[3] = { 111.1, 222.2, 333.3 };

	//函数指针,并且直接赋值了
	const double *(*p1)(const double *, int) = f1;
	auto p2 = f2;//使用C++11特性
	//const double *(*p2)(const double *, int) = f2;

	cout << "------------  1  ---------------" << endl;
	cout << "使用函数指针:\n";
	cout << "地址\n";
	cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;
	cout << p2(av, 3) << ": " << *p2(av, 3) << endl;

	//------------------------
	//auto 在list初始化时不管用!
	const double *(*pa[3])(const double *, int) = { f1, f2, f3 };
	//but auto works for initializing to a single value
	//pb a pointer to first element of pa
	auto pb = pa;
	//C++11等价于const double *(**pb)(const double *,int ) = pa;
	cout << "\n\n------------  2  ---------------" << endl;
	cout << " 使用数组指针:\n";
	cout << "地址" << endl;
	for (int i = 0; i < 3; i++)
		cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;

	cout << "\n\n ------------  3  ---------------" << endl;
	cout << "使用指向指针的指针:\n";
	cout << "地址" << endl;
	for (int i = 0; i < 3; i++)
		cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;

	//指针指向数组指针会如何?
	cout << "\n\n------------  4  ---------------" << endl;
	cout << "使用指针指向数组指针" << endl;
	cout << "地址" << endl;
	auto pc = &pa;
	//C++11 等价于  const double *(*(pc)[3])(const double *,int) =&pa;
	cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl;
	//使用比较麻烦的方法声明
	const double *(*(*pd)[3])(const double *, int) = &pa;
	//store return value in pdb
	const double * pdb = (*pd)[1](av, 3);
	cout << pdb << ": " << *pdb << endl;
	//alternative notation 替代符号
	cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av,3) << endl;



	return 0;
}

const double *f1(const double* ar, int n)
{
	return ar;
}
const double *f2(const double ar[], int n)
{
	return ar + 1;
}
const double *f3(const double ar[], int n)
{
	return ar + 2;
}

猜你喜欢

转载自blog.csdn.net/qq_15698613/article/details/89434824