Using an enum as an array index

前端 未结 7 1443
忘了有多久
忘了有多久 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 13:00

    Using an enum is ok. But you don't have to specify values for every item. It's enough to specify the first value. I wouldn't assume that enums start at 0, because I've used compilers which used 1 as the starting value (not for PCs but some compilers for microcontrollers have some weird behavior). Also, you could get rid of the const:

    enum ButtonState {
        BUTTON_NORMAL = 0,
        BUTTON_PRESSED,
        BUTTON_CLICKED,
        NUM_BUTTON_STATES
    };
    
    0 讨论(0)
提交回复
热议问题