Archived

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

Outputting enumerations

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