Accessing enum members...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Accessing enum members...

    Hi - I am seeing enum members being used, with just the member's name, and not the enum name.

    for example:
    Code:
    typedef enum Fruit
    {
      Apple = 0,
      Orange,
      Banana = 0xFF
    } FruitIndex;
    
    
    int arrayInt[Orange]
    {
      more code here.....
    }
    Question 1 - Is Orange's index = 1 ??
    Question 2 - what do you think the 0xFF hex is fox??
    QUESTION 3 - in the array - why don't I have to put [FruitIndex.Oran ge] to access the member??

    thanks!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Since this looks like a homework question, I'll answer Socratically.

    1: Print it out and find out.
    2: I don't know, what is it for? The answer to 1 might help.
    3: Is there anything else that the name Orange would conflict with?

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      It is NOT a homework question. I am actually stuck with this at work! Please help someone...

      I just wrote that example code as a simple piece but it is NOT homework.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Oh, I see. Regardless, the answer to 3 is still fine as far as I know. If there's anything else called Orange, you need to qualify it. If there isn't, you don't to the best of my knowledge. To 1, I don't know offhand, you should print out the value and find out. And for 2, I really don't know, maybe somebody needed the last value to be 255 for whatever reason. You're gonna have to dig around in the other code to answer some of these, I expect.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by Laharl
          And for 2, I really don't know, maybe somebody needed the last value to be 255 for whatever reason. You're gonna have to dig around in the other code to answer some of these, I expect.
          Or since this is work all you have to do is look in the project documentation where such a decision will be clearly given with the reason for the decision.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            OK. The enum Fruit is a type.
            [code=cpp]

            Fruit var1 = Orange;
            Fruit var2 = Banana;
            [/code]

            The value represented by Orange is 1 and the value represented by Banana is 0xFF.

            However:
            [code=cpp]
            Fruit var1 = 1; //ERROR
            Fruit var2 = 0xFF //ERROR
            [/code]

            Even though Orange represents 1 it is really Fruit::Orange and that 1 is an int. Now you will get a compiler error saying it's not possible to convert an int to a Fruit. Ditto for var2.

            Should you do a typecast:
            [code=cpp]
            Fruit var1 = (Fruit)1; //ERROR
            Fruit var2 = (Fruit)0xFF //ERROR
            [/code]

            the compiler errors will go away but the results will be indeterminate. That is, don't do this. Also don't:
            [code=cpp]
            Fruit var3 = (Fruit)456; //REALLY MAJOR ERROR
            [/code]

            This is also indeterminate.

            Comment

            Working...