#defines for input flags

I know how strongly it is discouraged to use preprocessor statements in c++, but in a class which takes input from a joystick and buttons is it really worth declaring over 20 const ints for input flags. They're literally only ever used as part of a switch statement upon a button/joystick press event, no mathematical operations are performed on them or anything. I just declared them to increase readability since

1
2
3
4
5
6
7
8
switch (button) {
case 0:
    // code
    break;
case 1:
    // more code
    break;
// ..... 


is much less readable than

1
2
3
const int START = 1;
const int SELECT = 2;
// ..... 


1
2
3
4
5
6
7
8
switch (button) {
case START:
    // code
    break;
case SELECT:
   // more code
   break;
// ..... 


So my question is whether there is really a downside to using #defines here, memory is quite limited in this project so I don't want to create so many variables for just this one use.
If you know the values of the keys you could use enum{...} to declare the const values, otherwise I always thought that Hardware communication was what #define is reserved for. Just make sure that you use #ifndef...#endif to avoid complications.
Last edited on
static const variables can often be optimized out by the compiler. The only time they can't be is if you need a pointer to them.

But Computergeek's idea to use an enum is the better way to go here anyway.

1
2
3
4
5
6
enum
{
  START=1,
  SELECT=2,
  ...
};
Ofc why didn't I think of that! Thanks guys.
You should use an enumeration rather than #define:
1
2
3
4
5
enum joystick_flags {
	JOYSTICK_START = 1,
	JOYSTICK_SELECT,
	/* ... */
};

Note: You don't need to specify the value of each symbol in an enumerated list; they follow onwards, e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
enum sequence1 {
	ZERO = 0,
	ONE,
	TWO,
	THREE,
	FOUR,
	FIVE,
	SIX,
	SEVEN,
	EIGHT,
	NINE,
	TEN
};

will be defined correctly. In fact, AFAIK you don't even need to specify a first value (you could just have put ZERO, ONE, TWO but I think it's best to.

You can also change the value:
1
2
3
4
5
6
7
8
9
10
11
enum sequence2 {
	ZERO = 0,
	ONE,
	TWO,
	THREE,
	FOUR,
	FIVE,
	TEN = 10,
	ELEVEN,
	TWELVE,	
};
Topic archived. No new replies allowed.