Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 have the code below and remove the typedef?

enum Coordinate {
    CoordinateX = 0,    ///< X axis
    CoordinateY = 1,    ///< Y axis
    CPCoordinateZ = 2   ///< Z axis
};
like image 573
Joo Park Avatar asked Jan 17 '11 18:01

Joo Park


1 Answers

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).

like image 88
BoltClock Avatar answered Nov 02 '22 15:11

BoltClock