【发布时间】:2009-11-06 12:51:36
【问题描述】:
void (*func)(int(*[ ])());
【问题讨论】:
标签: c++ c function-pointers
void (*func)(int(*[ ])());
【问题讨论】:
标签: c++ c function-pointers
阅读毛茸茸的声明符的一般过程是找到最左边的标识符并找出路,记住[]和()在*之前绑定(即*a[]是一个指针数组,而不是一个指向数组的指针)。由于参数列表中缺少标识符,这种情况变得更加困难,但同样,[] 绑定在 * 之前,所以我们知道 *[] 表示一个指针数组。
所以,给定
void (*func)(int(*[ ])());
我们将其分解如下:
func -- func
*func -- is a pointer
(*func)( ) -- to a function taking
(*func)( [ ] ) -- an array
(*func)( *[ ] ) -- of pointers
(*func)( (*[ ])()) -- to functions taking
-- an unspecified number of parameters
(*func)(int(*[ ])()) -- returning int
void (*func)(int(*[ ])()); -- and returning void
实际情况如下:
/**
* Define the functions that will be part of the function array
*/
int foo() { int i; ...; return i; }
int bar() { int j; ...; return j; }
int baz() { int k; ...; return k; }
/**
* Define a function that takes the array of pointers to functions
*/
void blurga(int (*fa[])())
{
int i;
int x;
for (i = 0; fa[i] != NULL; i++)
{
x = fa[i](); /* or x = (*fa[i])(); */
...
}
}
...
/**
* Declare and initialize an array of pointers to functions returning int
*/
int (*funcArray[])() = {foo, bar, baz, NULL};
/**
* Declare our function pointer
*/
void (*func)(int(*[ ])());
/**
* Assign the function pointer
*/
func = blurga;
/**
* Call the function "blurga" through the function pointer "func"
*/
func(funcArray); /* or (*func)(funcArray); */
【讨论】:
这不是声明,而是声明。
它将func 声明为一个指向函数的指针,该函数返回void 并采用int (*[])() 类型的单个参数,它本身是一个指向返回int 并采用固定但未指定数量的参数的函数的指针。
cdecl 输出给你的小信:
cdecl> explain void (*f)(int(*[ ])());
declare f as pointer to function (array of pointer to function returning int) returning void
【讨论】:
是的:
$ cdecl 解释 void (* x)(int (*[])()); 将 x 声明为指向函数的指针 (指向函数返回 int 的指针数组)返回 void【讨论】:
func 这个名字。
void (*func)(blah); 是一个指向以blah 为参数的函数的指针,其中blah 本身就是int(*[ ])() 是一个函数指针数组。
【讨论】:
【讨论】:
Geordi 是一个 C++ 机器人,可以训练这个:
<litb> geordi: {} void (*func)(int(*[ ])());
<litb> geordi: -r << ETYPE_DESC(func)
<geordi> lvalue pointer to a function taking a pointer to a pointer to a nullary function
returning an integer and returning nothing
它可以做很多有用的事情,比如显示所有参数声明(实际上,这只是匹配原始C++ 语法规则名称):
<litb> geordi: show parameter-declarations
<geordi> `int(*[ ])()`.
让我们反其道而行之:
<litb> geordi: {} int func;
<litb> geordi: make func a pointer to function returning void and taking array of pointer to
functions returning int
<litb> geordi: show
<geordi> {} void (* func)(int(*[])());
如果你问它,它会执行你给它的任何东西。如果你受过训练但忘记了一些可怕的括号规则,你也可以混合使用 C++ 和 geordi 风格的类型描述:
<litb> geordi: make func a (function returning void and taking (int(*)()) []) *
<geordi> {} void (* func)(int(*[])());
玩得开心!
【讨论】: