Why do people use enums in C++ as constants while they can use const?

前端 未结 12 1452
梦毁少年i
梦毁少年i 2020-12-08 02:28

Why do people use enums in C++ as constants when they can use const?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 03:08

    I like the automatic behavior that can be used with enums, for example:

    enum {NONE, START, HEY, HO, LAST};
    

    Then it is easy to loop until LAST, and when a new state (or whatever is represented) is added, the logic adapts.

    for (int i = NONE; i < LAST; i++)
    {
        // Do stuff...
    }
    

    Add something...

    enum {NONE, START, HEY, WEE, HO, LAST};
    

    The loop adapts...

提交回复
热议问题