Teen Programmers Unite  
 

 

Return to forum top

C++: enum

Posted by 142857 [send private reply] at August 28, 2002, 11:47:51 PM

Enum in C++. What's the deal with the numbers assigned to different values? For instance,

enum value { THIS = 0, THAT = 5, THOSE = 10 };

and I know if that's correct. So what can you use the numbers for? Is there a rule for how big the numbers can be? Thank you

Posted by CViper [send private reply] at August 28, 2002, 11:52:59 PM

you can use those values as constants (instead of #define'ing 'em).

the only limit is the size of the variable to hold the constant (maybe some compilers got an internal limit too, but that shouldn't be anything to worry about...)

Posted by Psion [send private reply] at August 29, 2002, 07:52:37 AM

enum's are just int's with special typing information for convenience in avoiding using numbers to which you haven't assigned meanings.

Posted by RedX [send private reply] at August 29, 2002, 11:18:11 AM

For example:

enum colors {red = 10, blue, green, black};

would be the same as

const int red = 10;
const int blue = 11;
const int green = 12;
const int black = 13;

It's just a way to type less.

Posted by Cybermonk [send private reply] at August 29, 2002, 11:25:41 AM

yeah thats right !

Posted by Psion [send private reply] at August 29, 2002, 11:29:04 AM

It's not the same thing, because you should be able to get with the proper compiler settings a warning if you try to pass a plain int to a function that expects an enum colors.

Posted by Cybermonk [send private reply] at August 29, 2002, 11:39:12 AM

who cares. as long as it works fine.

Posted by RedX [send private reply] at August 29, 2002, 11:45:17 AM

Oeps, forgot to mention that.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.