-
Enum or Const Ints?
In my program I have something that would be like a status that is an int variable. Currently, the status is being set by just using a number relevant to the actual status. Instead of just writing status = 1, status = 2 it's better to make the numbers as names, right? Would it be better for all the status names to be const int or put it all into an enum? In code it'd be like:
Code:
const int STATUS_ACTIVE = 0, /*etc*/;
enum StatusEnum {STATUS_ACTIVE, /*etc*/};
Also, the status is used across mutliple files. Const ints could be externed, but for enum would you extern a single one or create multiple variables of the enum?
-
or #define STATUS_ACTIVE 0
-
Either the const int or the enum is fine (you can use an unnamed enum as well). I would just put the enum in a header file. Don't use the #define.
-
it really depends on what you want. first as Daved said, don't use #define.
in you case it looks like you have a bunch of related constants,
i.e. STATUS_ACTIVE, STATUS_DEAD, STATUS_ON_A_COFFEE_BREAK.
In this case, an enum is really best. In fact, you should use a StatusEnum everywhere you need this info in your code. The type system is there to help, after all.
OTOH, if you have a single constant, something like MAX_NUMBER_OF_BEERS_BEFORE_DRUNK then it's better as a const int. (IMHO)
Probably the most useful thing you can learn about these things is to scope them. Make everything as local as it can possibly be. That's why the macro is evil.
-
Thanks for the replies. They've given me a better understanding of when to use which.
-
use enum in combonation with namespaces. this way is better because enums are global to.
this is an example of my enum code
Code:
namespace BEGIN{enum ENUM{ POINTS = GL_POINTS,
LINE_LOOP = GL_LINE_LOOP,
LINE_STRIP = GL_LINE_STRIP,
TRIANGLES = GL_TRIANGLES>,
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
QUAD_STRIP = GL_QUAD_STRIP,
POLYGON = GL_POLYGON};}
...
static inline void Begin(const BEGIN::ENUM& mode) // inside someclass
{
glBegin(mode);
}
...
someclass.Begin(BEGIN::QUADS);
this is the way i prefer