Enumerated constants

I am trying to work my way through a "Teach yourself C++" book. In a section about variables there was code to illustrate the use of enumerated constants. According to the book, unless otherwise declared, enumerated constants have integer values beginning with 0.

In this code:


enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday };
Days today;
today = Wednesday;

I tried to do something outside the scope of the lesson:

today = 6;
std::cout << today;

Although I could get this to compile with warnings (g++ -fpermissive) the output for this code is 6 and not Saturday - which is the value for Days for 6.

Is there a way to let the program read the integer value and respond with the correct value of the variable typed as Days?

I know there are conditionals I can use, but that seems to obviate the data type defined in this code.

Replying on an iPad is annoyingly difficult.

Yes, but only with magic.

The simplest way. Is to keep enumerations automatic (0,1,2,...) and have an array of character string constants to match.

1
2
enum Days = { Sunday, Monday, ... };
const char* DayNames = { "Sunday", "Monday", ... };

Keeping the two synchronized is an issue, because in many minds it usually violates the One Definition Rule. (There's an easy macro trick to fix that though, if you want to know.)

Enumerations that have non-automatic values require extra complexity to map values to strings. And again there are ways to do this -- a std::map or an intermediary lookup table (the c version of a map) are probably the simplest.

Hope this helps.
Thanks. It just seemed odd that each value had a numerical value, but that value did not reference the variable. Just a quirk of the language it seems. Thanks for clearing that up. Now onto another chapter!
What did all the warnings tell you?
Invalid conversion from 'int' to 'main()::Days'
and Days defined as seen Duoas reply - line #1
There you go, you shouldn't use an int where an enum is expected, it may well compile but it gives you warnings. After all, you are programming the program so you should stick to the types as you have defined them.
If you read above, since these enum variables have a numerical value, it just seemed one should be able to refer to their numerical value and obtain the corresponding variable name. It seems this correspondence does not exist within C++, which is fine. As a newbie, I am trying to understand the extent and limitations of this language.
Yes agreed, enums are literals. If you declare an anum you should stick with it and not suddenly start using a int in its place.
C has traditionally been very slack about it. C++ tries to be more pedantic.

Enums are integers. But the purpose is to make them magic -- you aren't supposed to care what their value is.

This, of course, turns out to be rather naïve in many cases, as we very often want our enumerations to have specific values.

It could be argued that integer constants should be used instead... and while reasonable, it isn't what people do.

If you want to banter between an integer and an enum, use a typecast.


The best argument for being able to convert between an enum and some representative value (int or string or whatever) is serialization -- sometimes you just want to save and load state.
Topic archived. No new replies allowed.