LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2006, 01:48 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
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?
 
Old 06-08-2006, 02:27 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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...
 
Old 06-08-2006, 07:58 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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...
 
Old 06-09-2006, 12:36 PM   #4
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
Thanks guys.
 
Old 06-09-2006, 02:54 PM   #5
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Days of the week is a common example.
 
Old 06-11-2006, 01:13 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> 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.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if an int value is within valid range of enum? mic Programming 6 02-21-2006 11:02 AM
enum inheritance in C++? graemef Programming 2 01-27-2006 10:14 PM
HOTSWAP: where to find #ENUM signal? iflorea Programming 0 11-20-2005 06:58 AM
C Enum exvor Programming 3 09-27-2005 02:51 PM
C, enum confusion cigarstub Programming 2 09-27-2005 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration