Using an enum as an array index

前端 未结 7 1450
忘了有多久
忘了有多久 2020-12-13 12:30

I have this enum:

enum ButtonState {
    BUTTON_NORMAL = 0,
    BUTTON_PRESSED = 1,
    BUTTON_CLICKED = 2
};

const u8 NUM_BUTTON_STATES = 3;
7条回答
  •  春和景丽
    2020-12-13 12:54

    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.

提交回复
热议问题