在不使用单独的typedef的情况下声明函数指针数组的语法是什么?

Max*_*xpm 44 c++ arrays syntax typedef function-pointers

可以像这样创建函数指针数组:

typedef void(*FunctionPointer)();
FunctionPointer functionPointers[] = {/* Stuff here */};
Run Code Online (Sandbox Code Playgroud)

在不使用typedef?的情况下创建函数指针数组的语法是什么?

Arm*_*yan 84

arr    //arr 
arr [] //is an array (so index it)
* arr [] //of pointers (so dereference them)
(* arr [])() //to functions taking nothing (so call them with ())
void (* arr [])() //returning void 
Run Code Online (Sandbox Code Playgroud)

所以你的回答是

void (* arr [])() = {};
Run Code Online (Sandbox Code Playgroud)

但自然,这是一个不好的做法,只需使用typedefs:)

额外: 想知道如何声明一个3指针数组的数组,该函数采用int并返回指向4个指针数组的指针,该指针指向带有double和返回char的函数?(那有多酷,啊?))

arr //arr
arr [3] //is an array of 3 (index it)
* arr [3] //pointers
(* arr [3])(int) //to functions taking int (call it) and
*(* arr [3])(int) //returning a pointer (dereference it)
(*(* arr [3])(int))[4] //to an array of 4
*(*(* arr [3])(int))[4] //pointers
(*(*(* arr [3])(int))[4])(double) //to functions taking double and
char  (*(*(* arr [3])(int))[4])(double) //returning char
Run Code Online (Sandbox Code Playgroud)

:))

  • +1,但对于这种受虐狂,有cdecl.org.`:)` (24认同)
  • 天啊,我的眼睛.@ _ @ (9认同)
  • 谢谢马特奥;cdecl.org 是一个很棒的网站! (2认同)

Log*_*ldo 14

请记住"使用模拟".所以要使用你说的阵列

 (*FunctionPointers[0])();
Run Code Online (Sandbox Code Playgroud)

正确?因此要声明它,你使用相同的:

 void (*FunctionPointers[])() = { ... };
Run Code Online (Sandbox Code Playgroud)


Eri*_*rik 5

用这个:

void (*FunctionPointers[])() = { };
Run Code Online (Sandbox Code Playgroud)

与其他所有内容一样,您可以将 [] 放在名称后面。