Why do you use typedef when declaring an enum in C++?
In C, declaring your enum the first way allows you to use it like so:
TokenType my_type;
If you use the second style, you'll be forced to declare your variable like this:
enum TokenType my_type;
As mentioned by others, this doesn't make a difference in C++. My guess is that either the person who wrote this is a C programmer at heart, or you're compiling C code as C++. Either way, it won't affect the behaviour of your code.
It's a C heritage, in C, if you do :
enum TokenType
{
blah1 = 0x00000000,
blah2 = 0X01000000,
blah3 = 0X02000000
};
you'll have to use it doing something like :
enum TokenType foo;
But if you do this :
typedef enum e_TokenType
{
blah1 = 0x00000000,
blah2 = 0X01000000,
blah3 = 0X02000000
} TokenType;
You'll be able to declare :
TokenType foo;
But in C++, you can use only the former definition and use it as if it were in a C typedef.
You do not need to do it. In C (not C++) you were required to use enum Enumname to refer to a data element of the enumerated type. To simplify it you were allowed to typedef it to a single name data type.
typedef enum MyEnum {
//...
} MyEnum;
allowed functions taking a parameter of the enum to be defined as
void f( MyEnum x )
instead of the longer
void f( enum MyEnum x )
Note that the name of the typename does not need to be equal to the name of the enum. The same happens with structs.
In C++, on the other hand, it is not required, as enums, classes and structs can be accessed directly as types by their names.
// C++
enum MyEnum {
// ...
};
void f( MyEnum x ); // Correct C++, Error in C