Best way to create "modes"

Aug 4, 2009 at 10:46pm
Hello,
I am writing an app and I was wondering how can I implement
"mode" constants, like the openGl's GL_*, for example, I need for a part
of my program the constants BUTTON_PRESSED, BUTTON_RELEASED, etc, for another
NEXT_CARD, PREVIOUS_CARD and so on. The ideas I came up with were

*create only one enum of modes and put in a header to include where necessary
*create several enums, one for each header
*use #define CONSTANT either in one header file to include or in each header file
separately

So which is the best practice, if any?

Thanks in advance.
Last edited on Aug 5, 2009 at 12:01am
Aug 5, 2009 at 12:22am
It depends exactly on how these modes are to be used.

I would lean in favor of typed enums for different practices. OpenGL does plain #defines, which makes it vulnerable to misuse. IE, you can pass any mode to any function which takes a mode, even if it makes no sense.

For your particular instance, I would recommend something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
enum BUTTONSTATE
{
  BUTTON_PRESSED,
  BUTTON_RELEASED
};

enum CARD  // come up with a better name for this probably
{
  NEXT_CARD,
  PREV_CARD
};

//etc 


This way you can't accidentally pass 'BUTTON_PRESSED' to a function which takes a 'CARD' (or something similar) because the compiler will bark at you.
Aug 5, 2009 at 1:34pm
Thanks for your reply. I see your point. In this case I would
make these enums members of the respective classes ?
Aug 5, 2009 at 2:44pm
That's what I do.
Topic archived. No new replies allowed.