Jump to content
  • Advertisement
Sign in to follow this  
DJLink

typedef enum declaration confusion

This topic is 2215 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:


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 this post


Link to post
Share on other sites
Advertisement
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!