Questions about enum. What would be a good use of enum?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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)
> 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.