Using an enum as an array index

前端 未结 7 1459
忘了有多久
忘了有多久 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:53

    It's fine, but I'd want to do some bounds checking on the array, as if someone adds another ButtonState, you'll have a problem.

    Also, the elements of the colors array are immutable, so maybe look at using a different collection to array so that you can enforce that immutability. Maybe a Dictionary

提交回复
热议问题