The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

Enum fun... by MrTrick2005-05-11 17:54:19
  There's unfortunately no... by williamashbless 2005-05-11 18:09:10
...automatic way to do this. An enum is just a source code tag. The way to do it in C++ is the same as Euphoria:

enum
{
ONE = 1,
TWO = 2,
PURPLE = 3,
FIVE = 4,
GOLDFISH = 5,
NUM_VALUES = 5
};
const char *lookup[NUM_VALUES+1]={"","ONE", "TWO", "PURPLE", "FIVE", "GOLDFISH"};

Of course, if you leave off the numeric assignments in the enum, it auto-enums from 0, and NUM_VALUES ends up being the correct value, which is a nice property of the auto-enumeration-from-zero.

You can, of course, leave the NUM_VALUES out of the enum, and remove it from the array brackets, as long as you keep the lookup table the right size.

Keep in mind that arrays in C/C++ are indexed from zero, and hence the dummy entry at the beginning of the array.

There is another alternative: Using a preprocessor tuple.

#define MY_ENUMS \
MY_ENUM_TUPLE(ONE)\
MY_ENUM_TUPLE(TWO)\
MY_ENUM_TUPLE(PURPLE)\
MY_ENUM_TUPLE(FIVE)\
MY_ENUM_TUPLE(GOLDFISH)\
//end of the list

enum
{
#define MY_ENUM_TUPLE(x) x ,
MY_ENUMS
#undef MY_ENUM_TUPLE
NUM_VALUES
};

const char *lookup[NUM_VALUES]=
{
#define MY_ENUM_TUPLE(x) #x ,
MY_ENUMS
#undef MY_ENUM_TUPLE
};

which, if you're autonumbering from zero, automatically sets up both the enums and strings from a single initial mult-line preprocessor item.

Again, you can get rid of NUM_VALUES here if you have no use for it -- and in this case, this guarantees the string table is the right size, and everything's indexed from 0.

It is, of course, possible to do this _without_ the autonumbering, so if you absolutely need that, we can discuss workarounds.

[ Reply ]

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)