Difference between Enum and Define Statements

后端 未结 18 2476
终归单人心
终归单人心 2020-11-30 00:27

What\'s the difference between using a define statement and an enum statement in C/C++ (and is there any difference when using them with either C or C++)?

For exampl

相关标签:
18条回答
  • 2020-11-30 00:50

    If you only want this single constant (say for buffersize) then I would not use an enum, but a define. I would use enums for stuff like return values (that mean different error conditions) and wherever we need to distinguish different "types" or "cases". In that case we can use an enum to create a new type we can use in function prototypes etc., and then the compiler can sanity check that code better.

    0 讨论(0)
  • 2020-11-30 00:52

    There is little difference. The C Standard says that enumerations have integral type and that enumeration constants are of type int, so both may be freely intermixed with other integral types, without errors. (If, on the other hand, such intermixing were disallowed without explicit casts, judicious use of enumerations could catch certain programming errors.)

    Some advantages of enumerations are that the numeric values are automatically assigned, that a debugger may be able to display the symbolic values when enumeration variables are examined, and that they obey block scope. (A compiler may also generate nonfatal warnings when enumerations are indiscriminately mixed, since doing so can still be considered bad style even though it is not strictly illegal.) A disadvantage is that the programmer has little control over those nonfatal warnings; some programmers also resent not having control over the sizes of enumeration variables.

    0 讨论(0)
  • 2020-11-30 00:54

    enums are more used for enumerating some kind of set, like days in a week. If you need just one constant number, const int (or double etc.) would be definetly better than enum. I personally do not like #define (at least not for the definition of some constants) because it does not give me type safety, but you can of course use it if it suits you better.

    0 讨论(0)
  • 2020-11-30 00:55

    In addition to the good points listed above, you can limit the scope of enums to a class, struct or namespace. Personally, I like to have the minimum number of relevent symbols in scope at any one time which is another reason for using enums rather than #defines.

    0 讨论(0)
  • 2020-11-30 00:57

    #define is a preprocessor command, enum is in the C or C++ language.

    It is always better to use enums over #define for this kind of cases. One thing is type safety. Another one is that when you have a sequence of values you only have to give the beginning of the sequence in the enum, the other values get consecutive values.

    enum {
      ONE = 1,
      TWO,
      THREE,
      FOUR
    };
    

    instead of

    #define ONE 1
    #define TWO 2
    #define THREE 3
    #define FOUR 4
    

    As a side-note, there is still some cases where you may have to use #define (typically for some kind of macros, if you need to be able to construct an identifier that contains the constant), but that's kind of macro black magic, and very very rare to be the way to go. If you go to these extremities you probably should use a C++ template (but if you're stuck with C...).

    0 讨论(0)
  • 2020-11-30 00:57

    Creating an enum creates not only literals but also the type that groups these literals: This adds semantic to your code that the compiler is able to check.

    Moreover, when using a debugger, you have access to the values of enum literals. This is not always the case with #define.

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