Why Do You Use Typedef When Declaring an Enum in C++

Why does C allow the use of enum as a variable?

In this declaration

enum
{
DETAILS_A =0x00U,
DETAILS_B =0x01U,
DETAILS_C =0x02U
}u8_Details;

you declared the variable u8_Details of an unnamed enumeration type in the file scope.

You may assign a value to the variable as to any other mutable variable.

As for your assumption

u8_Details u8_Details = 42;

then it is wrong. u8_Details is not a type specifier. It is an identifier of a variable of an unnamed enumeration type.

Correct way to declare typedef enum in C?

From the C Standard (6.7.2.2 Enumeration specifiers)

3 The identifiers in an enumerator list are declared as constants that
have type int and may appear wherever such are permitted.127) An
enumerator with = defines its enumeration constant as the value of the
constant expression. If the first enumerator has no =, the value of
its enumeration constant is 0. Each subsequent enumerator with no =
defines its enumeration constant as the value of the constant
expression obtained by adding 1 to the value of the previous
enumeration constant.
(The use of enumerators with = may produce
enumeration constants with values that duplicate other values in the
same enumeration.) The enumerators of an enumeration are also known as
its members.

So for example all these declarations are equivalent

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
} k4abt_sensor_orientation_t;

However for more readability I would prefer this declaration

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

especially when an enumeration contains many enumerators.

why need typedef in the C++ enum definition

It's for C users. Basically when you go the typedef way, in C you can say

Test myTest;

whereas when you just used enum Test, you'd have to declare a variable like this:

enum Test myTest; 

It's not needed if only C++ is used though. So it might also just be written by a C programmer who is used to this style.

What is a typedef enum in Objective-C?

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 kCircle, kRectangle, and kOblateSpheroid 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 this must 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 enum anonymous 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 kCircle, kRectangle, and kOblateSpheroid). You can assign a ShapeType variable another value by casting, though, so you have to be careful when reading enum values.

Finally, kCircle, kRectangle, 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.



Related Topics



Leave a reply



Submit