Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an enum as an array index

Tags:

c++

enums

I have this enum:

enum ButtonState {     BUTTON_NORMAL = 0,     BUTTON_PRESSED = 1,     BUTTON_CLICKED = 2 };  const u8 NUM_BUTTON_STATES = 3; 

In my Button class I have member variables ButtonState state; and ButtonColors colors[NUM_BUTTON_STATES];. When drawing the button, I use colors[state] to get the colours for whatever state the button is in.

My questions:

  1. Is this good programming style? Is there a better way to do it? (I usually only use enums with switch statements... using an enum as an array index doesn't feel right.)
  2. Do I have to specify the values of the enum? It seems to start from 0 by default and increment by 1 but is it guaranteed to work that way in all compilers?
like image 486
Paige Ruten Avatar asked Dec 31 '08 23:12

Paige Ruten


People also ask

Can I use enum as array index?

It is perfectly normal to use an enum for indexing into an array. You don't have to specify each enum value, they will increment automatically by 1. Letting the compiler pick the values reduces the possibility of mistyping and creating a bug, but it deprives you of seeing the values, which might be useful in debugging.

Can we use enum as array?

To get all enum values as an array, pass the enum to the Object. values() method, e.g. const values = Object.

Can you index an enum in C?

You can't do that. A C enum is not much more than a bunch of constants. There's no type-safety or reflection that you might get in a C# or Java enum . Show activity on this post.

Should I use enum or array?

The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value.


1 Answers

Is this good programming style?

I think so. I do the same thing quite frequently.

Is there a better way to do it?

class Button { public:     // Used for array indexes!  Don't change the numbers!   enum State {     NORMAL = 0,     PRESSED,     CLICKED,     NUMBER_OF_BUTTON_STATES   }; }; 

Drawback is that NUMBER_OF_BUTTON_STATES is now a valid Button::State value. Not a big issue if you are passing these values around as ints. But trouble if you are actually expecting a Button::State.

Using an enum as an array index doesn't feel right.

It's fine. Just DOCUMENT it, so the next guy knows what's going on! (That's what comments are for.)

Do I have to specify the values of the enum?

With no '=' assignment, enum's are supposed to start at zero and increment upwards.

If a enum entry has an '=' assigned value, subsequent non '=' enum entries continue counting from there.

Source: The Annotated C++ Reference Manual, pg 113

That said, I like to specify the initial value just to make the code that much clearer.

like image 108
Mr.Ree Avatar answered Oct 13 '22 10:10

Mr.Ree