Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

rugal

Outputting enumerations

This topic is 5286 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 all I was wondering is there an easy way to output the name of an enumeration? Tried google without luck. Say I have: enum Colours {red, yellow, blue} Colour MyColour = yellow; I know you can do: if (MyColour == yellow) { glPrint("Your colour is Yellow") } But is there a simpler way without the if statement? Put simply can you cast an enum value to a string? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Don''t think so! Each enum is afaik converted to an int at compile time, which means that the name is only for the programmer''s sake.



DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!

My games: DracMan | Swift blocks

Share this post


Link to post
Share on other sites
quote:
Original post by rugal
But is there a simpler way without the if statement? Put simply can you cast an enum value to a string?


There''s no easy way, but it can atleast be done automatically with a bit of preprocessor magic. Wouldn''t recommend it unless you have atleast a few dozen entries though.

in colors.h

color(red),
color(green),
color(blue)


in color.h

enum Color
{
#define color(id) id
#include "colors.h"
#undef color
};

extern const char *ColorNames[];


in color.c

const char *ColorNames[] =
{
#define color(id) #id
#include "colors.h"
#undef color
};

Share this post


Link to post
Share on other sites

  • 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!