What is a typedef enum in Objective C?

原帖: http://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c

===============================================================================================================================

I don't think I fundamentally understand what a enum is, and when to use it.

For example:

typedef enum {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType;

What is really being declared here?

===============================================================================================================================

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 kCirclekRectangle, andkOblateSpheroid are being declared as integral constants.

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. In C and Objective-C (but not C++), any references to thismust be preceded with the enum keyword. 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 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 enumanonymous and only declare it with the typedef name:

typedef enum { ... } tagname;

Now, in this case, we're declaring ShapeType to be a typedef'ed name of an anonymous enumeration.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 kCirclekRectangle, and kOblateSpheroid). You can assign a ShapeType variable another value by casting, though, so you have to be careful when reading enum values.

Finally, kCirclekRectangle, and kOblateSpheroid are declared as integral constants in the global namespace. 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.

===============================================================================================================================

A user defined type that has the possible values of  kCircle kRectangle , or  kOblateSpheroid . The values inside the enum (kCircle, etc) are visible outside the enum, though. It's important to keep that in mind ( int i = kCircle;  is valid, for example).

===============================================================================================================================

A enum declares a set of ordered values - the typedef just adds a handy name to this. The 1st element is 0 etc.

typedef enum {
Monday=1,
...
} WORKDAYS;

WORKDAYS today = Monday;

The above is just a enumeration of shapeType tags.

===============================================================================================================================






### 如何在C/C++中正确引用`typedef enum`定义的枚举类型 #### 定义与声明方式 为了提高代码可读性和简洁性,通常使用`typedef`关键字简化枚举类型的命名。下面展示了一种常见的做法: ```c // 定义枚举类型并利用 typedef 关键字创建别名 typedef enum { false, true } BOOLEAN; ``` 上述语句不仅定义了一个名为匿名枚举体,还通过`typedef`给这个枚举指定了一个新的名称`BOOLEAN`[^1]。 #### 变量声明 当有了带有别名的枚举类型之后,可以直接用该别名来声明变量而无需再重复`enum`关键词: ```c BOOLEAN end_flag, match_flag; // 声明两个 BOOLEAN 类型的变量 ``` 这种方式使得后续代码更加清晰易懂,并减少了冗余输入。 #### 结合结构体使用 有时也会看到将`typedef enum`同结构体一起使用的场景,这有助于构建复杂的数据模型: ```c typedef struct tagNode { BOOLEAN isValid; int value; } Node; Node nodeInstance = {true, 42}; // 初始化结构体成员 if (nodeInstance.isValid == true) { printf("The node is valid.\n"); } ``` 这里展示了如何在一个结构体内嵌入由`typedef enum`定义过的布尔标志位字段。 #### 明确指定底层数据类型(C++特有) 对于现代标准下的C++编程而言,还可以进一步控制枚举项所占用的空间大小,从而优化内存利用率: ```cpp #include <iostream> typedef enum : unsigned char { // 指定底层存储为单字节无符号字符 RED, GREEN, BLUE } Color; int main() { Color myColor = RED; std::cout << "Size of Color: " << sizeof(Color) << " bytes\n"; switch(myColor){ case RED: std::cout<<"It's red."; break; default: std::cout<<"Other color."; } return 0; } ``` 此段程序片段说明了自C++11起支持显式设置枚举的基础整数类型特性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值