这个typedef语句是什么意思?

rsg*_*mon 76 c++ typedef

在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)

  • 你的导师是对的.这很难阅读,这是你*不应该使用的功能. (17认同)
  • 在我多年编程C++的过程中,我不知道这一点!我想我的导师不相信有必要学习它,我从来没有发现任何关于它的东西.+1 (5认同)

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:后

typedef int MILES , * KLICKSP ;
Run Code Online (Sandbox Code Playgroud)

建筑

MILES distance ;
extern KLICKSP metricp ;
Run Code Online (Sandbox Code Playgroud)

都是正确的声明; 距离的类型metricp是"指针指向int." - 例子

  • 据我所知,答案是正确的.为什么会有反对票呢? (13认同)

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).