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

前端 未结 12 1426
梦毁少年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:17

    Enums are distinct types, so you can do type-oriented things like overloading with them:

    enum Color { Red,Green,Blue };
    enum Size { Big,Little };
    
    void f( Color c ) {
    }
    
    void f( Size s ) {
    }
    
    int main() {
        f( Red );
        f( Big );
    }
    

提交回复
热议问题