Three things are being declared here: an anonymous enumerated type is declared, ShapeType
is being declared a typedef for that anonymous enumeration, and the three names kCircle
, kRectangle
, and kOblateSpheroid
are being declared as integral constants.
(这里声明了三件事:声明了一个匿名枚举类型, ShapeType
被声明为该匿名枚举的typedef,并且三个名称kCircle
, kRectangle
和kOblateSpheroid
被声明为整数常量。)
Let's break that down.
(让我们打破它。)
In the simplest case, an enumeration can be declared as (在最简单的情况下,枚举可以声明为)
enum tagname { ... };
This declares an enumeration with the tag tagname
.
(这声明了带有标记tagname
的枚举。)
In C and Objective-C (but not C++), any references to this must be preceded with the enum
keyword. (在C和Objective-C(但不是 C ++)中,对此的任何引用都必须以enum
关键字开头。)
For example: (例如:)
enum tagname x; // declare x of type 'enum tagname'
tagname x; // ERROR in C/Objective-C, OK in C++
In order to avoid having to use the enum
keyword everywhere, a typedef can be created:
(为了避免在任何地方使用enum
关键字,可以创建一个typedef:)
enum tagname { ... };
typedef enum tagname tagname; // declare 'tagname' as a typedef for 'enum tagname'
This can be simplified into one line:
(这可以简化为一行:)
typedef enum tagname { ... } tagname; // declare both 'enum tagname' and 'tagname'
And finally, if we don't need to be able to use enum tagname
with the enum
keyword, we can make the enum
anonymous and only declare it with the typedef name:
(最后,如果我们不需要能够将enum tagname
与enum
关键字一起使用,我们可以使enum
匿名并仅使用typedef名称声明它:)
typedef enum { ... } tagname;
Now, in this case, we're declaring ShapeType
to be a typedef'ed name of an anonymous enumeration.
(现在,在这种情况下,我们将ShapeType
声明为匿名枚举的typedef名称。)
ShapeType
is really just an integral type, and should only be used to declare variables which hold one of the values listed in the declaration (that is, one of kCircle
, kRectangle
, and kOblateSpheroid
). (ShapeType
实际上只是一个整数类型,并且只应用于声明包含声明中列出的值之一的变量(即kCircle
, kRectangle
和kOblateSpheroid
)。)
You can assign a ShapeType
variable another value by casting, though, so you have to be careful when reading enum values. (但是,您可以通过强制转换为ShapeType
变量指定另一个值,因此在读取枚举值时必须小心。)
Finally, kCircle
, kRectangle
, and kOblateSpheroid
are declared as integral constants in the global namespace.
(最后, kCircle
, kRectangle
和kOblateSpheroid
在全局命名空间中声明为整数常量。)
Since no specific values were specified, they get assigned to consecutive integers starting with 0, so kCircle
is 0, kRectangle
is 1, and kOblateSpheroid
is 2. (由于未指定特定值,因此将它们分配kCircle
0开头的连续整数,因此kCircle
为0, kRectangle
为1, kOblateSpheroid
为2。)