struct{ const int EAST = 0; const int SOUTH = 1; const int WEST = 2; const int NORTH = 3; };
Who in God's name would use a struct like that? That is not what structs are there for.
The enum
keyword is for where you want to have different, related constants enumerated without having to use #define
on everything. Logically, it should only be used when the number behind that constant has no real meaning. (For example, nobody should care that direction_north
is 2, or 3, or -45, because the code should only really refer to it as direction_north
). If you have to refer directly to a value like that, you're probably using enum
incorrectly.
Here is an example of how I used an enum
in a game I am currently programming.
//enum for statuses returned by loadgame()
enum loadstatus_t
{
load_success, //Indicates that the savegame was successfully loaded.
load_nofile, //Indicates the save doesn't exist.
load_badversion, //Indicates the save is from too new or old of a version.
load_smallscreen //Indicates the user's screen dimensions can't fit the save file on screen
};
A struct
should be created when you have several variables that 'go together'. For example, you can have this mess:
float redapple_weight;
float redapple_diameter;
int redapple_price;
bool redapple_isrotting;
float greenapple_weight;
float greenapple_diameter;
int greenapple_price;
bool greenapple_isrotting;
float crabapple_weight;
float crabapple_diameter;
int crabapple_price;
bool crabapple_isrotting;
See how much of a mess that is? You basically have to copy-paste a lot of …