why is typedef used with enum type?
why is typedef needed in the code below?
typedef enum _Coordinate {
CoordinateX = 0, ///< X axis
CoordinateY = 1, ///< Y axis
CPCoordinateZ = 2 ///< Z axis
} Coordinate;
why not just 开发者_如何学Gohave the code below and remove the typedef?
enum Coordinate {
CoordinateX = 0, ///< X axis
CoordinateY = 1, ///< Y axis
CPCoordinateZ = 2 ///< Z axis
};
If you don't typedef
your enum, in your other code you have to refer to it as enum Coordinate
instead of just Coordinate
, even though you know Coordinate
is an enum. It's just to eliminate the redundancy of the enum
keyword when referring to it, I guess.
To make it clearer, here's what it would look like if you typedef
it separately from declaring it:
enum _Coordinate {
CoordinateX = 0,
CoordinateY = 1,
CPCoordinateZ = 2
};
// You map the enum type "enum _Coordinate" to a user-defined type "Coordinate"
typedef enum _Coordinate Coordinate;
This also applies to C structs and enums (Obj-C enums are just C enums anyway).
精彩评论