Why can two different enum enumeration-constants have the same integer value?

前端 未结 5 1165
半阙折子戏
半阙折子戏 2021-01-31 14:08

I know that if I defined an enum weekday like this:

enum weekday {
    MON,
    TUE,
    WED,
    THU,
    FRI,
};

Then, MON would internally e

5条回答
  •  心在旅途
    2021-01-31 14:27

    Just to complement on other answers, I'll give you a practical example of how using the same value for different enumerations on a given enum is widely useful:

    enum slots_t {
        SLOT_FIRST = 0,
        SLOT_LEFTARM = SLOT_FIRST,
        SLOT_RIGHTARM = 1,
        SLOT_TORSO = 2,
        SLOT_LEFTLEG = 3,
        SLOT_RIGHTLEG = 4,
        SLOT_LAST = SLOT_RIGHTLEG
    };
    

    Then you can do in your code:

    for (int i = SLOT_FIRST; i <= SLOT_LAST; ++i) { }
    

提交回复
热议问题