A question about "enum" I should have asked long ago

enem day{mon, tue, wed, thur, fri, sat, sun};

day week=mon;

Ok, so week equals zero. I don’t understand the purpose for enums except to initialize a long list of integer variables in sequence. Is that all enums good for?

Well, the big reason (for me) to use enum’s is that it lets the compiler help you find errors that would otherwise never be found.

For example:

#include

int main(int argc, char *argv)
{
enum day {MON, TUE, WED, THU, FRI, SAT, SUN};

day today;

switch (today)
{
    case MON:
        cout << "Ugh. Monday sucks" << endl;
        break;

    case FRI:
        cout << "TGIF!" << endl;
        break;
}

return 0;

}

My compiler will complain about this:

$ g++ -o test -Wall test.cpp
test.cpp: In function int main(int, char **)': test.cpp:18: warning: enumeration value TUE’ not handled in switch
test.cpp:18: warning: enumeration value WED' not handled in switch test.cpp:18: warning: enumeration value THU’ not handled in switch
test.cpp:18: warning: enumeration value SAT' not handled in switch test.cpp:18: warning: enumeration value SUN’ not handled in switch

It will also warn about this often made mistake:

enum day {MON, TUE, WED, THU, FRI, SAT, SUN};

void do_something(day some_day)
{

}

int main(int argc, char *argv)
{
do_something(67);

return 0;

}

$ g++ -o test -Wall test.cpp
test.cpp: In function int main(int, char **)': test.cpp:12: conversion from int’ to `enum day’

If you were to use #define MON 0 etc, this would slip by undetected.

enums also help when you are debugging.
Instead of seeing “day = 5”, most modern debuggers will tell you “day = FRI”, which is a lot more meaningful.

Hope this helps.

[This message has been edited by rts (edited 04-16-2001).]

Very interesting.

I have never really learned my compiler MSVC very well. I just learned enough to get by. In fact, that’s what I’ve done with all my compilers.

I’ve heard that you can even make the MSVC compiler give you a report on how long functions take to execute. Beats me how to do that.

I’m a perfect example of why home teaching isn’t always the best way to learn programming. I don’t know all those little details that you would learn in school. I’ve gotten by for 5 years without those little things so far…I just need to take time and learn the rest :/.

Although I do have an Honours B.Sc. in Comp Sci and completed two years of master’s work before joining the work force, the vast majority of my learning was at home on my own time.

There was no class at university about the benefits of enum and all that. It was learned via osmosis, mostly from collegues and from reading freely available code .

So… don’t despair… I think I safely speak for most everyone on this board when I say you learn more on your own than in school.

I definatly agree with that because it’s pure hands on all the time.

May I ask, rts, what OS you’re running? Just curious seeing g++ as your compiler. I run g++ on Linux and VC++ on Win98 but I was curious as to whether you have cygwin/g++ (and whether OpenGL is hardware accelerated under it).

Hi
You can also use encapsulate the enums in classes, which makes your code much readable, and helps avoid name collisions. For example
class ITopologyEditor
{
typedef enum { GLOBAL = 0, VERTICES = 1, EDGES = 2, FACES = 3, MODTYPE_COUNT = 4} ModifiedTypeEnum;
virtual void Modify (ITopologyEditor::ModifiedTypeEnum eModify) = 0;
};

Then you can access the enum trough the class specificator:
CTopEditor topEditor;
topEditor.Modify(ITopologyEditor::EDGES);

Also this enum is public, but you may typedef a private enum, which will be accessible only to class member functions.
Enums help doing readable bit flag operations too:
class CMyDlg
{
enum {ENABLE_FINISH = 0x00000001,
ENABLE_CANCEL = 0x00000002,
ENABLE_HELP = 0x00000004,
ENABLE_APPLY = 0x00000008};

void PlaceButtons (uint nButtons);
};

CMyDlg dlg;
dlg.PlaceButtons(CMyDlg::ENABLE_APPLY | CMyDlg::ENABLE_HELP);

Regards
Martin

Originally posted by ffish:
May I ask, rts, what OS you’re running? Just curious seeing g++ as your compiler. I run g++ on Linux and VC++ on Win98 but I was curious as to whether you have cygwin/g++ (and whether OpenGL is hardware accelerated under it).

My OpenGL work is done under GNU/Linux.

However, I also use cygwin g++ and VC++ on Win 2000, but not for OpenGL work.