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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Razmig K

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

    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
  • Jakob Bieling

    #2
    Re: C++ programmers! How do you use your 'enum's ?

    "Razmig K" <strontium90@po stmaster.co.uk> wrote in message
    news:3cad08b8.0 308071321.79771 dde@posting.goo gle.com...[color=blue]
    > 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.[/color]

    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)


    Comment

    • Jim

      #3
      Re: C++ programmers! How do you use your 'enum's ?

      Noah Roberts <nroberts@donte mailme.com> wrote in message news:<3F3343A8. 9090601@dontema ilme.com>...[color=blue]
      > Razmig K wrote:[color=green]
      > > 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[/color]
      >
      > 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...a nd 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[/color]

      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?

      Comment

      • Noah Roberts

        #4
        Re: C++ programmers! How do you use your 'enum's ?

        [color=blue][color=green]
        >>enum { red, black }
        >>enum { empty, all = 0, pawn, canon, cart, horse, elephant, guard, general };[/color]
        >
        >
        > 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?[/color]


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

        NR

        Comment

        • Dave Rahardja

          #5
          Re: C++ programmers! How do you use your 'enum's ?

          >Razmig K wrote:[color=blue][color=green]
          >> 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[/color][/color]

          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_FRO BNITZ = 0x04000000,
          REG1_ENABLE_BOO MBAH = 0x02000000,
          REG1_BLATHER_MA SK = 0x00f00000,
          REG1_BLATHER_SH IFT = 20
          };

          Then I use the bits like so...

          reg1 = reg1 | REG1_ENABLE_FRO BNITZ; // Enable Frobnitz
          reg1 = reg1 & ~REG1_ENABLE_BO OMBAH; // Disable Boombah
          reg1 = reg1 & ~REG1_BLATHER_M ASK;
          reg1 = reg1 | ((blather_value ) << REG1_BLATHER_SH IFT);

          Comment

          • Boris Kolpackov

            #6
            Re: C++ programmers! How do you use your 'enum's ?

            strontium90@pos tmaster.co.uk (Razmig K) writes:
            [color=blue]
            > This is just a small survey for the common (and uncommon) nontrivial
            > uses of the aforementioned C++ construct.[/color]

            The following URL can be of some interest to you



            hth,
            -boris

            --
            Boris Kolpackov boris@kolpackov .net http://kolpackov.net
            3072R/AE084F1D - F608 942F 312E D82E 5B84 0407 C880 290B AE08 4F1D

            Comment

            Working...