• 13
• 16
• 27
• 9
• 9

# typedef enum declaration confusion

This topic is 2026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone. So I never noticed this until today.

I usually declare something like:

 typedef enum { Action_IDLE = 0, Action_BORED = 1, Action_WALK = 2 }Object_Action; 

And I can do this

 Object_Action action = Action_IDLE; 

Looking at "Class View" in Visual Studio I noticed that instead of the list having the name "Object_Action" it's named as "__unnamed_enum_012b_1" .

But if I declare it like

 typedef enum Object_Action <- notice the diference { Action_IDLE = 0, Action_BORED = 1, Action_WALK = 2 }Object_Action; 

The name is displayed properly and they both compile. So what's the difference then? From a logical point of view.

Thanks

##### Share on other sites
The typedef enum { ... } Object_Action; is a C idiom because enums, like structs, have a separate namespace by default:
 // No typedefs enum Object_Action { /* ... */ }; // Need to use the "enum" keyword to compile this in C enum Object_Action action = Action_IDLE; 
A common variant on this idiom is to given the enum a name and a typedef: typedef enum Foo { ... } Foo, essentially the "workaround" you are using.

C++ removed this namespacing, I imagine because because approximately nobody wants to use it. In C++ you can just have:
 enum Object_Action { /* ... */ }; Object_Action action = Action_IDLE;