有一段代码没看懂,C++的,哪位达人帮我一下好吗?

来源:百度知道 编辑:UC知道 时间:2024/06/04 08:12:46
下面是一段介绍typedef用法的:

为复杂的声明定义一个新的简单的别名。方法是:在原来的声明里逐步用别名替换一部分复杂声明,如此循环,把带变量名的部分留到最后替换,得到的就是原声明的最简化版。举例:

原声明:void (*b[10]) (void (*)());

变量名为b,先替换右边部分括号里的,pFunParam为别名一:

typedef void (*pFunParam)();

再替换左边的变量b,pFunx为别名二:

typedef void (*pFunx)(pFunParam);

原声明的最简化版:

pFunx b[10];

但我的问题是,原申明void (*b[10]) (void (*)());到底申明了什么?这是一个函数?还是一个什么变量?或是一个什么指针?我没看懂原申明的意思。

原定是:
一个函数指针的数组,这个函数指针指向的函数为:
返回void,传入一个函数指针。
而这个传入的函数指针指向的函数为:没有参数,返回void。

原理参见Thinking in C++,

void (*funcPtr)();

To review, “start in the middle” (“funcPtr is a ...”), go to the right (nothing there – you're stopped by the right parenthesis), go to the left and find the ‘*’ (“... pointer to a ...”), go to the right and find the empty argument list (“... function that takes no arguments ... ”), go 214 Thinking in C++ www.BruceEckel.com to the left and find the void (“funcPtr is a pointer to a function that takes no arguments and returns void”).

对于这个例子首先到中间 "b",向右看为[10],说明b是一个数组;向左看是"*",向右看,是参数列表(void(*)()),再向左看是void,也就是函数指针的返回值。
针对参数列表再次使用这个方法分析就可以了。

给你几个更复杂的分析一下试试,
/* 1. */ void * (*(*fp1)(int))[10];
/* 2. */ float (*(*fp2)(int,int,float))(int);
/* 3. */ typedef double (*(*(*fp3)())[10])()