Item 17. 函数与数组声明上的比较

Item 17. Dealing with Function and Array Declarators

函数与数组的声明能够放在一起来比较,是因为它们有相同的遭遇,同是天涯沦落人。

------------------------------------------------------------------
1、两种函数声明
例如:
 int *f1(); // 返回类型为int* 的函数
 int (*fp1)(); // 指向返回类型为int的函数之指针

2、两种数组
例如:
const int N = 12;
int *a1[N]; // 有N个int*的数组
int (*ap1)[N]; // 指向有N个int的数组之指针

3、推而广之
int (**ap2)[N]; // ptr to ptr to array of N ints
int *(*ap3)[N]; // ptr to array of N int *
int (**const fp2)() = 0; // const ptr to ptr to func
int *(*fp3)(); // ptr to func that returns int *

4、当函数遇上数组的时候
int (*afp2[N])(); // array of N ptr to func that returns int

另一种写法
typedef int (*FP)(); // ptr to func that returns int
FP afp3[N]; // array of N FP, same type as afp2

5、当引用和const出现后:
int aFunc( double ); // func
int (&rFunc)(double) = aFunc; // ref to func
int (*const pFunc)(double) = aFunc; // const ptr to func

 

posted on 2005-04-13 20:49  张大大123  阅读(101)  评论(0编辑  收藏  举报

导航