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

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

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

12条回答
  •  一整个雨季
    2020-12-08 03:12

    Before compiler vendors implemented the ISO/IEC 14882:1998 C++ standard, this code to define a constant in a class scope resulted in a compile error:

    class Foo {
        static const int MAX_LEN = 80;
        ...
    };
    

    If the constant is an integer type, a kludgy work around is define it in an enum inside the class:

    class Foo {
        enum {
            MAX_LEN = 80
        };
        ...
    };
    

提交回复
热议问题