Connecting Tech Pros Worldwide Help | Site Map

Accessing enum members...

Member
 
Join Date: Jul 2008
Posts: 62
#1: Sep 5 '08
Hi - I am seeing enum members being used, with just the member's name, and not the enum name.

for example:
Expand|Select|Wrap|Line Numbers
  1. typedef enum Fruit
  2. {
  3.   Apple = 0,
  4.   Orange,
  5.   Banana = 0xFF
  6. } FruitIndex;
  7.  
  8.  
  9. int arrayInt[Orange]
  10. {
  11.   more code here.....
  12. }
  13.  
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.Orange] to access the member??

thanks!
Expert
 
Join Date: Sep 2007
Posts: 856
#2: Sep 5 '08

re: Accessing enum members...


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?
Member
 
Join Date: Jul 2008
Posts: 62
#3: Sep 5 '08

re: Accessing enum members...


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.
Expert
 
Join Date: Sep 2007
Posts: 856
#4: Sep 5 '08

re: Accessing enum members...


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.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#5: Sep 5 '08

re: Accessing enum members...


Quote:

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.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#6: Sep 5 '08

re: Accessing enum members...


OK. The enum Fruit is a type.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Fruit var1 = Orange;
  3. Fruit var2 = Banana;
  4.  
The value represented by Orange is 1 and the value represented by Banana is 0xFF.

However:
Expand|Select|Wrap|Line Numbers
  1. Fruit var1 = 1;     //ERROR
  2. Fruit var2 = 0xFF  //ERROR
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. Fruit var1 = (Fruit)1;     //ERROR
  2. Fruit var2 = (Fruit)0xFF  //ERROR
  3.  
the compiler errors will go away but the results will be indeterminate. That is, don't do this. Also don't:
Expand|Select|Wrap|Line Numbers
  1. Fruit var3 = (Fruit)456;  //REALLY MAJOR ERROR
  2.  
This is also indeterminate.
Reply