(C++)深入探讨函数指针及讲解C++11新特性关键字auto和关键字typedef

本文详细探讨了C++中的函数指针用法,强调了pa和&pa之间的区别,解释了它们在地址和类型上的差异。同时,介绍了C++11的新特性关键字`auto`,通过示例展示了如何根据初始值自动推断变量类型。最后,讨论了`typedef`关键字,用于创建类型别名,简化类型定义。
摘要由CSDN通过智能技术生成

程序例子:分三个部分

#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 (void)
{
	double av[3] = {1112.3, 1542.6, 2227.9};
	
	//PART 1
	//定义函数指针
	const double *(*p1)(const double *, int) = f1;
	auto p2 = f2;		//自动分配数据类型  相当于const double *(*p2)(const double *, int) = f2;   
	cout << "Using pointers to functions: " << endl;
	cout << "Address      Value" << endl;
	cout << (*p1)(av,3) << " : " << *(*p1)(av,3) << endl;
	cout << p2(av,3) << " : " << *(*p2)(av,3) << endl;
	
	//PART2
	//定义数组,由指针构成,其3个元素是指针类型
	const double *(*pa[3])(const double *, int) = {f1, f2, f3};
	auto pb = pa;
	cout << endl << "Using an array of pointers to functions: " << endl;
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄TimTim仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值