[Tutor] enumeration in C++
Paul Sidorsky
paulsid@shaw.ca
Tue, 02 Apr 2002 11:00:06 -0700
Nicole Seitz wrote:
> What's enumeration good for? Can someone give me a few examples when I would
> use enumeration?Years ago, when I was TRYING to learn C, I came across enum,
> but never found out its purpose.
Enumeration is very handy for things like mode or state constants, for
example:
typedef enum
{
StBeginning, StMiddle, StMiddle2, StEnd, NUMSTATES
} states;
or
typedef enum
{
ModeIcon, ModeSmallIcon, ModeList, ModeDetails, NUMMODES
} modes;
I always like to include the total in there since then if I insert new
modes/states it gets updated automatically.
In C++ (if I remember correctly) you also get type protection if a
variable is defined to be an enum, for example:
modes currentmode;
...
currentmode = ModeSmallIcon; // OK
...
currentmode = 2; // Illegal, even though 2==ModeList
Interestingly enough, despite having grown up with C for over a decade,
I haven't missed enumerations in Python. But they can be rather handy
sometimes.
--
======================================================================
Paul Sidorsky Calgary, Canada
paulsid@shaw.ca http://members.shaw.ca/paulsid/