LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Questions about enum. What would be a good use of enum? (http://www.linuxquestions.org/questions/programming-9/questions-about-enum-what-would-be-a-good-use-of-enum-452666/)

RHLinuxGUY 06-08-2006 02:48 AM

Questions about enum. What would be a good use of enum?
 
I recently notice enum's in a new book I picked up. C++ Programming Language 3rd edition. I understand that whatever declarations you put between the curly braces when defining a enum, it is increased by one from 0 up. I also understand that this was supposed to be a better way to define enumerations then before with c, where you had to create "magic numbers". Example:

#define ONE 1
#define TWO 2
#define THREE 3

or however you would like to do it.

So far, the only things I can think of that would be of some use, would be in a calculator program, (such as the one shown in the book). Maybe constants for a switch statement, or a if statement. Maybe for a menu in a program. I can see how this could be easier then defining the type and declaring a name. I'm also picking up that it would be easier to read when someone is reviewing your code. Like "aha, I see, he has some sort of 'calculation' function or etc. that uses this enum", but then I don't know, and that is what my question is about. What exactly would you use enumerations for?

chrism01 06-08-2006 03:27 AM

Potentially, anything that naturally has a monotonic increasing integer associated with it eg deck of cards (actually, you'd use 2 enums: face value and suit).
You could use these to index via a "string value" ie enum name, into an array, rather than having to remember what each number pts to.
More self-documenting; superficially looks like a hash...

graemef 06-08-2006 08:58 AM

The number may not be relevant for enums to be useful. For example I use them when listing configuration options. I have a configuration file that holds details such as background colour, text colour etc. I have some code that reads specific options from the config file, this will map the enum with the specific entry (typically via a - large - switch statement) This appears quite clumsy but the advantage is that in my code whenever I want to use the actual background colour I will have a call similar to the following:
Code:

Pref::get(BackgroundColour);
This is, I believe, very readable in the code and obviously easy for the user to change the settings.

With enums you can also set the value which also has its uses, if you have a small set of values enums can easily solve it for you. Take the three classic font styles, bold, italic and underline, these could be assigned the values of 1, 2 and 4. Now simple bit operators can be used to see if the style is bold and underline (1+4 =5 or in binary 001 + 100 = 101)

Their use is limited to your imagination... :)

RHLinuxGUY 06-09-2006 01:36 PM

Thanks guys.

Vagrant 06-09-2006 03:54 PM

Days of the week is a common example.

xhi 06-11-2006 02:13 PM

> I understand that whatever declarations you put between the curly braces when defining a enum, it is increased by one from 0 up.

you can define the value of an enum, ex
enum Direction { up=12, down=6, left=9, right=3 };

and the usage of enums is for ease of understanding for the programmer. there does not have to be a correlation between the name and number for it to be a good usage. and the usages for them can be anything that can be logically grouped together and identified by a name. common things are colors, directions, and as graeme mentioned config params.


All times are GMT -5. The time now is 03:15 PM.