在C++参考页面中,他们提供了一些typedef示例,我试图理解它们的含义.
// simple typedef
typedef unsigned long mylong;
// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)
所以我理解的是简单的typedef(第一个声明).
但是他们用第二个宣告什么(下面重复)?
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)
特别是什么(&fp)(int, mylong)
意思?
Mik*_*our 96
它一次声明了几个typedef,就像你可以一次声明几个变量一样.它们都是基于的类型int
,但有些被修改为复合类型.
让我们把它分成单独的声明:
typedef int int_t; // simple int
typedef int *intp_t; // pointer to int
typedef int (&fp)(int, ulong); // reference to function returning int
typedef int arr_t[10]; // array of 10 ints
Run Code Online (Sandbox Code Playgroud)
Yu *_*Hao 41
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)
相当于:
typedef int int_t;
typedef int *intp_t;
typedef int (&fp)(int, mylong);
typedef int arr_t[10];
Run Code Online (Sandbox Code Playgroud)
在C++ 11标准中实际上有一个类似的例子:
C++ 11 7.1.3 的
typedef
说明符甲
typedef
-name不引入新类型的方式的class
声明(9.1)或enum
声明does.Example:后Run Code Online (Sandbox Code Playgroud)typedef int MILES , * KLICKSP ;
建筑
Run Code Online (Sandbox Code Playgroud)MILES distance ; extern KLICKSP metricp ;
都是正确的声明; 距离的类型
metricp
是"指针指向int
." - 例子
Ama*_*osh 32
如果您拥有该cdecl
命令,则可以使用它来揭开这些声明的神秘面纱.
cdecl> explain int (&fp)(int, char)
declare fp as reference to function (int, char) returning int
cdecl> explain int (*fp)(int, char)
declare fp as pointer to function (int, char) returning int
Run Code Online (Sandbox Code Playgroud)
如果没有cdecl
,您应该能够以通常的方式安装它(例如在Debian类型的系统上使用sudo apt-get install cdecl
).