gettings values in an enum?

two questions
1) how do i cycle through the value of an enum and check each value and print it if if want to? can i do it like i would an array?>

for ex say i have:

enum me {LO, LA, LI};

if i wanted to get LO, could i do cout << me[0] ?
and if i wanted to get all the values in the enum, could i just cycle through it the same i would an array?

2) also i do not firsthand what items i want to add to the enum, that will be done during the program execction since im reading from a file. so how would i do that? does enum have like push() function that i can use during execution to keep pushing items in it?
Last edited on
I think you're confusing enums with arrays. enums are just a bundle of symbolic constants that share a type. For example, a common use is to return an error from a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
//with enums
enum error_code{
    NO_ERROR=0,
    SOMETHING_ERROR_HAPPENS
};

error_code function();

//without enums:
#define NO_ERROR 0
#define SOMETHING_ERROR_HAPPENS 1

int function();

I think your confusion stems from thinking that enum is a type. It's not. It's a type declaration, just like class. The type in your example is 'me'. Iterating over an enum makes no more sense than iterating over a class.
As for me and error_code, they both behave a lot like integer types (they have a couple restrictions, such as having to be explicitly casted to be converted between integer types), so they don't have any members, either. Not that it would make sense for them to have members.
Last edited on
so enums do not have functions?
so enums do not have functions?

No, they don't.
http://www.cplusplus.com/doc/tutorial/other_data_types/#enum
Topic archived. No new replies allowed.