Alex Rivera | Logout

Why do people use enums in C++ as constants while they can use const?

Asked 2009-05-22T20:44:45.490
37

Why do people use enums in C++ as constants when they can use const?

Edit
Report

2 Answers

10

I like the automatic behavior that can be used with enums, for example:

enum {NONE, START, HEY, HO, LAST};

Then it is easy to loop until LAST, and when a new state (or whatever is represented) is added, the logic adapts.

for (int i = NONE; i < LAST; i++)
{
    // Do stuff...
}

Add something...

enum {NONE, START, HEY, WEE, HO, LAST};

The loop adapts...

answered 2009-05-22T21:01:42.960
0

Using an enum documents the valid choices in a terse manner and allows the compiler to enforce them.

If they are using enum store global constants, like Pi, for example, then I don't know what their goal is.

answered 2009-05-22T20:59:33.947

Your Answer