Enum's let you define your own data types that can only have the specific values you specify. For example the variable type
bool
could be expressed as (of course bool is a keyword and you couldn't actually do this, but bear with me for the sake of example)
|
enum bool { FALSE, TRUE };
|
Enum's are indexed starting at 0 and incrementing by one for each value (how odd that 0 means false and 1 means true in C++, and these are the indexes of true and false in the enum above :o). You can specify otherwise if you want to however by assigning elements a specific index.
|
enum bool { FALSE = 1, TRUE = 0 };
|
You can also assign integers to enums like this:
If the enum bool was defined as I defined it in the original example, test would hold a value of 1.
You asked how it's used so here's an example. Let's say you were making a 2D game, and the player in it must be facing in one of four directions at all times, up, down, left and right. Then you could define an enum like this:
|
enum playerDirection { UP, DOWN, LEFT, RIGHT} player; // remember you can define objects by putting them before the semicolon at the end :-)
|
Then you're player will always be facing one direction, and you can change the direction to whatever you want. For example if the person playing the game pressed the right arrow key you could say
You could also say
These would both have the same affect :0. Hope this helps.
Also you might want to take a good read here:
http://www.cprogramming.com/tutorial/enum.html
EDIT: Dangit someone posted before me!