By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

C++ programmers! How do you use your 'enum's ?

P: n/a
Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C++ construct.
It's posted by an average C++ programmer who's curious about how his
elder colleagues utilize this feature of C++ in their implementations
based on diverse programming styles in diverse application domains.

Thank you for your interest.

Best regards,
//rk
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Razmig K" <st*********@postmaster.co.uk> wrote in message
news:3c**************************@posting.google.c om...
Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C++ construct.
It's posted by an average C++ programmer who's curious about how his
elder colleagues utilize this feature of C++ in their implementations
based on diverse programming styles in diverse application domains.


Though I am far from being an 'elder' colleague, here goes.

I can only think of occasions, where I used enums to give constants a
name, as well as to group them (the constants). Suppose I was writing a
text-processor which enables you to align the text. All possible options to
align that text would go into one enum. Another (slightly different) usage
would be putting all options for formatting the font (bold, italics,
underline etc) in another enum. Now you do not only pass a single value, but
I may also combine them.

Recently, I also used enums as named indeces into an array. This does
not really fit into the group-and-give-name category and I do not think it
is used often. Personally, I have used it only once so far.

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

P: n/a
Jim
Noah Roberts <nr******@dontemailme.com> wrote in message news:<3F**************@dontemailme.com>...
Razmig K wrote:
Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C++ construct.
It's posted by an average C++ programmer who's curious about how his
elder colleagues utilize this feature of C++ in their implementations
based on diverse programming styles in diverse application domains.

Thank you for your interest.

Best regards,
//rk


I used to use enums to declair methods only capable of accepting or
returning certaing integer values. It really become more pain than it
is worth most of the time in many of the applications I put it to; plus
I am not too sure it actually works that way :P

Mainly now I just use enums to declair sets of constants that I want to
be distinct, many of which are used as induces to arrays. For instance
I use two enums in the interface to my Board class in a chess engine I
am working on:

enum { red, black }
enum { empty, all = 0, pawn, canon, cart, horse, elephant, guard, general };

pawns = boardInstance[red][pawn]; // returns the red pawn board.

and so on...

Things like that make my code more pleasant, and it needs things like
that :P

I also use it to refer to things that don't necissarily have to be in
any order, or even next to each other, but must be distinct and/or are
related in idea. For instance, my interface modes are specified in an
enum; not because they care what the numbers are, but because they are
related and it groups symbols for readability...and there is a lot less
typing than with a bunch of #defines.

NR

PS. No, I am not an "elder" programmer. Especially with C++ as I have
been more of a C or Objective-C man in the past. And yes, that is a
pretty trivial use :P


Do you realize that in your example above (using GCC 3.2.2 on Solaris)
the value of empty ends up being 0? Did you expect it to equal -1?
Jul 19 '05 #3

P: n/a
enum { red, black }
enum { empty, all = 0, pawn, canon, cart, horse, elephant, guard, general };

Do you realize that in your example above (using GCC 3.2.2 on Solaris)
the value of empty ends up being 0? Did you expect it to equal -1?

No, I expected it to be 0 as it should be in any and all C/C++ compilers
afaik.

NR

Jul 19 '05 #4

P: n/a
>Razmig K wrote:
Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C++ construct.
It's posted by an average C++ programmer who's curious about how his
elder colleagues utilize this feature of C++ in their implementations
based on diverse programming styles in diverse application domains.

Thank you for your interest.

Best regards,
//rk


I use ENUMS to give names to integer constants, to give a type to a set of
related constants, and to give names to bit fields within integers (e.g. in a
hardware device register).

For example:

enum REG1_bits_T {
REG1_ENABLE_FROBNITZ = 0x04000000,
REG1_ENABLE_BOOMBAH = 0x02000000,
REG1_BLATHER_MASK = 0x00f00000,
REG1_BLATHER_SHIFT = 20
};

Then I use the bits like so...

reg1 = reg1 | REG1_ENABLE_FROBNITZ; // Enable Frobnitz
reg1 = reg1 & ~REG1_ENABLE_BOOMBAH; // Disable Boombah
reg1 = reg1 & ~REG1_BLATHER_MASK;
reg1 = reg1 | ((blather_value) << REG1_BLATHER_SHIFT);

Jul 19 '05 #5

P: n/a
st*********@postmaster.co.uk (Razmig K) writes:
This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C++ construct.


The following URL can be of some interest to you

http://kolpackov.net/projects/c++/enum/

hth,
-boris

--
Boris Kolpackov bo***@kolpackov.net http://kolpackov.net
3072R/AE084F1D - F608 942F 312E D82E 5B84 0407 C880 290B AE08 4F1D
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.