Sign in to follow this  

Printing enum names?

This topic is 4763 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

So I'm using enums for defining events for an event system. All fine and dandy, but when it comes to logging or outputting the event id to some sort of text output I obviously just get the numberic value. Now, I realize an enum is basically a number disguised as a name in the IDE, but is this name lost after compilation? Is there a way to print out the actual enums name instead of the number? My guess is no cuz the compiled code probably has no clue that its anything other than a number, but I figured I'd ask anyway. If the answer is indeed no, what might be a good way to get the name. I had thought of a big char * array being all initialized to the names of the events so when I print out the event instead of using the event id I would use eventnames[eventid], but the problem is that it's easy to forget to add a string for new events you add to the enum. If there's another more automated way that would cover adding new events and (hopefully)their names get added automatically, I'd rather do that. Any ideas? J

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
Now, I realize an enum is basically a number disguised as a name in the IDE, but is this name lost after compilation?
Yep.
Quote:
Is there a way to print out the actual enums name instead of the number?
Nope.

Quote:
If the answer is indeed no, what might be a good way to get the name.
Your idea is the only sound one: a string table. You can add an assert to your program that requires that the length of the string table equal the size of the enumeration so that forgetting to add a string would be an error.

Alternatively, you can pray for introspection in C++ or use an introspection library, but it's overkill.

Share this post


Link to post
Share on other sites

This topic is 4763 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