Thread: Enum example, plz?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002

    Smile Enum example, plz?

    can anyone give me a sipmle enumerator example and also a simple class example for me to learn and study from. I have checked within here already and it gave me really great info on the basics. Can anyone help me, please?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User
    Join Date
    Apr 2002
    An "interesting" point about "enum's" is that they involve "ranges" of values. You can assign a value to an enum element rather than settling for the default values:

    enum color { red = 3, blue = 5, green = 9 };

    Wierd part? (Very wierd, actually.):

    color orange = color (7); adds 'orange' to the 'enum'. (Don't thank me. Thank Bjarne Stroustrup!) Please note that the rvalue must be preceded by the data "type" (the enum name), or else you messed up!

    We need to think "binary" here.

    Binary numbers run: 0, 1 , 2, 4, 8, 16, 32, 64, 128...well, you get the point.

    When we assign "values" to our enum elements, we effectively set a "range" of values that enum values can fall into. Specifically, our last element is 'green = 9'. Back to our binary example, we must look to the next highest value (binarily speaking) beyond the value given within the enum declaration/definition to see what the "upper" limit is.

    Since '9' > '8' - I'm good at this - the next highest binary value is 16. Now, the range for our enum values becomes 1-16, or, in C++ terms, 0- 15.

    Therefore, we may assign any additional value to our 'enum' as long as it falls between 0 and 15. Obviously, if our highest original value were 18, for argument's sake, we could go between 0 and 31.

    (Probably a lot more than you needed/wanted/cared to know, huh?)


    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 10:29 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM