When do you use enumerated type?

Aug 12, 2010 at 9:35pm
I am curious, when do you ever use this data type?
I need some examples because I don't know when should I be "encourage" to use it.

I know how to write it but does not know the purpose and usage.

Thank you.
Aug 12, 2010 at 10:03pm
I rarely, if ever, use enums. I might use them once the new standard is ratified and we have strongly typed enums. http://en.wikipedia.org/wiki/C%2B%2B0x#Strongly_typed_enumerations

Other than that, they're handy as a type that defines a limited set of integers.
Aug 13, 2010 at 8:49am
You can use them as a nice way to define some in class constants:

1
2
3
4
5
6
7
8
9
10
11
12
13
class something {
public:
    enum ret_val {
        SUCCESS = 0,
        FAILURE = 1
    };
    //...
};

int main() {
    something::ret_val = something::FAILURE;
    //...
}
Aug 13, 2010 at 5:27pm
Hello firedraco,

Can such constants be defined in a struct?
Aug 13, 2010 at 7:01pm
Can such constants be defined in a struct?

In C++, a struct is the same as a class, the only difference being that a struct's members are public by default.
Aug 13, 2010 at 11:30pm
I use them when making games to hold the 'state' of the player/enemy/bullets/whatever.

1
2
3
4
5
6
7
enum state_t
{
     resting,
     walking,
     jumping,
     etc.....
};


Then when the entity is being drawn, the number from the entity's state can represent which row in the texture to loop through for animation, so if it is resting the 1st row of frames are used, if it is walking the 2nd are used and so on.

I have little experience in anything other than games I'm afraid but that's how I use them.
Aug 14, 2010 at 9:58pm
Enumerations are mainly used for states (like in the example given above). They are special in the way that they have a "built-in check", since only defined values are accepted. You should normally not have to use them, but whenever you should, do it.
Topic archived. No new replies allowed.