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.

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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,`typedef enum`是**结合了typedefenum两个关键字来创建一个新的枚举类型别名的方法**。 首先,让我们分别了解typedefenum的基本概念: - **Enum(枚举)**:枚举是C语言中的一种数据类型,用于声明一组命名的整数值。它提供了一种方式来定义一个变量,这个变量只能从预定义的一组值中选取。例如,可以定义一个枚举类型来表示一周的七天,或者不同的颜色等。 - **Typedef**:typedef是C语言的一个关键字,它允许程序员为现有的数据类型创建一个新的名称。这样做的目的是提高代码的可读性和方便性。例如,可以用typedef为int类型创建别名,如`typedef int INTEGER;`,之后就可以用INTEGER代替int来声明变量。 当结合使用typedefenum时,可以创建一个新的枚举类型的别名。这样做的好处是,可以在程序中多次使用这个别名来声明枚举类型的变量,而不需要每次都写出整个枚举类型的定义。这不仅可以减少打字工作量,还可以提高代码的清晰度和一致性。 此外,在使用typedefenum时,需要注意以下几点: - **Typedef不产生新类型**:typedef只是为现有类型提供了一个新的名称,它并不创造新的数据类型。 - **Enum的值是常量**:通过enum定义的值是真正的常量,它们在编译时被分配具体的整数值。 - **使用typedef enum时的注意事项**:在使用typedefenum定义别名时,应该确保别名的名称清晰地反映了它所代表的枚举类型的含义,以避免混淆。 综上所述,`typedef enum`在C语言中是一个强大的工具,它允许程序员创建易于理解和使用的枚举类型别名,从而提高代码的可维护性和可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值