今天看到enum 和 typedef, 在两个小节中书中分别出现了2个例子,如下
enum direction {north, south, east, west};
typedef enum {north, south, east, west} direction;
不禁产生疑问,这两个有什么区别,仔细对照了一下,发现是这样的:
大同小异,
同: 都是申明了一个枚举类型。
异:在使用该枚举类型定义变量的时候,语法不一样,举例如下:
1 enum direction {north, south, east, west};
3 enum direction facing = north;
4
5 typedef enum {north, south, east, west} direction;
7 direction facing = north;
3 enum direction facing = north;
4
5 typedef enum {north, south, east, west} direction;
7 direction facing = north;
本文对比了C/C++中enum直接定义枚举类型与通过typedef定义枚举类型的两种方式,并给出了具体的使用示例。
585

被折叠的 条评论
为什么被折叠?



