Non assign enum in the range

Is this a legit thing to do?


Define enum SPORT { basketball, baseball, football, hockey, tennis, handball, soccer, shooting, swimming, gymnastics}

int default = 14;

SPORT sport = SPORT(default);


Since the there are 10 sports in enum it goes up to 9 by default. The range of SPORT should be defined by 2^4-1 =16-1 = 15. So if I initialize sport to be 14 will it be an actual value or will it still be a garbage value?
Yes, you can do what you just did. The value would be 14, but that somewhat defeats the purpose of using an enum.
But you can't do int value = baseball;, nor can you do Sport s = 15;

If you want to force comparison with values inside that enum only, consider using enum class.

You can also have an enum start at any number you want to by having the first element be equal to that starting point. enum e { first = 15, second, third, fourth };.
randompersonhere,

It looks like you know that enums start at zero. If you want to change the numbers you will have to write it this way enum SPORT { basketball = 2, ... and everything after basketball will be one greater.

If you want put this code in a program and see what happens:
1
2
3
4
5
6
int default = 14;
enum SPORT { basketball, baseball, football, hockey, tennis, handball, soccer, shooting, swimming, gymnastics };
SPORT sport = SPORT(default);
cout << hockey << endl;
cin.get();
exit(0);

change the cout to any name in the enum and see what number printed out is.

The code SPORT sport = SPORT(default); appears to have no effect. Actually it does not make any sense to me. Not sure what is going o there, but it does not look right.

Hoe the helps some,

Andy
Topic archived. No new replies allowed.