Sign in to follow this  

typedef enum declaration confusion

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. So I never noticed this until today.

I usually declare something like:

[CODE]
typedef enum
{
Action_IDLE = 0,
Action_BORED = 1,
Action_WALK = 2
}Object_Action;
[/CODE]

And I can do this

[CODE]
Object_Action action = Action_IDLE;

[/CODE]

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

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

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

Thanks

Share this post


Link to post
Share on other sites
The typedef enum { ... } Object_Action; is a C idiom because enums, like structs, have a separate namespace by default:
[code]
// No typedefs
enum Object_Action { /* ... */ };

// Need to use the "enum" keyword to compile this in C
enum Object_Action action = Action_IDLE;
[/code]
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:
[code]
enum Object_Action { /* ... */ };

Object_Action action = Action_IDLE;
[/code]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this