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

前端 未结 12 1421
梦毁少年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 02:59

    An enumeration implies a set of related constants, so the added information about the relationship must be useful in their model of the problem at hand.

    0 讨论(0)
  • 2020-12-08 03:02

    One reason is that const requires more typing:

    enum { Val1, Val2, Val3 };
    

    ...versus...

    const int Val1=0, Val2=1, Val3=2;
    
    0 讨论(0)
  • 2020-12-08 03:03

    It's partly because older compilers did not support the declaration of a true class constant

    class C
    {
      const int ARealConstant = 10;
    };
    

    so had to do this

    class C
    {
      enum { ARealConstant = 10 };
    };
    

    For this reason, many portable libraries continue to use this form.

    The other reason is that enums can be used as a convenient syntactic device to organise class constants into those that are related, and those that are not

    class DirectorySearcher
    {
      enum options
      {
        showFiles = 0x01,
        showDirectories = 0x02,
        showLinks = 0x04,
      };
    };
    

    vs

    class Integer
    {
       enum { treatAsNumeric = true };
       enum { treatAsIntegral = true };
       enum { treatAsString = false };
    };
    
    0 讨论(0)
  • 2020-12-08 03:04

    Bruce Eckel gives a reason in Thinking in C++:

    In older versions of C++, static const was not supported inside classes. This meant that const was useless for constant expressions inside classes. However, people still wanted to do this so a typical solution (usually referred to as the “enum hack”) was to use an untagged enum with no instances. An enumeration must have all its values established at compile time, it’s local to the class, and its values are available for constant expressions. Thus, you will commonly see:

    #include <iostream>
    using namespace std;
    
    class Bunch {
      enum { size = 1000 };
      int i[size];
    };
    
    int main() {
      cout << "sizeof(Bunch) = " << sizeof(Bunch) 
           << ", sizeof(i[1000]) = " 
           << sizeof(int[1000]) << endl;
    }
    

    [Edit]

    I think it would be more fair to link Bruce Eckel's site: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-08 03:04

    Some debuggers will show the enumeration name instead of its value when debugging. This can be very helpful. I know that I would rather see day_of_week = MONDAY than day_of_week = 1.

    0 讨论(0)
提交回复
热议问题