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

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

    Enums are more descriptive when used. Consider:

    int f(int fg, int bg)
    

    versus

     int f(COLOR fg, COLOR bg)
    

    In addition, enums give a bit more type-safety, because

    • integers are not implicitly convertible to enum types
    • enum of one type is not implicitly convertible to enum of another type

提交回复
热议问题