在C++参考页面中,他们提供了一些类型定义示例,我正在尝试理解它们的含义。
// simple typedef
typedef unsigned long mylong;
// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
所以我理解了简单的typedef (第一个声明)。
但是他们对第二个(在下面重复)声明了什么呢?
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
特别是(&fp)(int, mylong)
是什么意思?
发布于 2014-02-27 15:30:43
它一次声明几个typedefs,就像你可以一次声明几个变量一样。它们都是基于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
发布于 2014-02-27 15:32:52
如果您有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
如果你没有cdecl
,你应该能够以通常的方式安装它(例如,在Debian类型的系统上,使用sudo apt-get install cdecl
)。
发布于 2014-02-27 15:34:59
typedef定义了一个新类型,以便在代码中使用,就像速记一样。
typedef typename _MyBase::value_type value_type;
value_type v;
//use v
这里的typename让编译器知道value_type是_MyBase中的一个类型,而不是一个对象。
::是类型的作用域。这有点像"is in“所以”value_type“在”_MyBase“中。或者也可以被认为是包含。
可能的重复项:C++ - meaning of a statement combining typedef and typename
https://stackoverflow.com/questions/22061750
复制相似问题