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

R

Razmig K

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
 
J

Jakob Bieling

Razmig K said:
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
 
J

Jim

Noah Roberts said:
Razmig said:
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?
 
N

Noah Roberts

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
 
D

Dave Rahardja

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top