Difference between Enum and Define Statements

后端 未结 18 2477
终归单人心
终归单人心 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 01:05

    While several answers above recommend to use enum for various reasons, I'd like to point out that using defines has an actual advantage when developing interfaces. You can introduce new options and you can let software use them conditionally.

    For example:

    
        #define OPT_X1 1 /* introduced in version 1 */
        #define OPT_X2 2 /* introduced in version  2 */
    
    

    Then software which can be compiled with either version it can do

    
        #ifdef OPT_X2
        int flags = OPT_X2;
        #else
        int flags = 0;
        #endif
    
    

    While on an enumeration this isn't possible without a run-time feature detection mechanism.

    0 讨论(0)
  • 2020-11-30 01:08

    enum defines a syntactical element.

    #define is a pre-preprocessor directive, executed before the compiler sees the code, and therefore is not a language element of C itself.

    Generally enums are preferred as they are type-safe and more easily discoverable. Defines are harder to locate and can have complex behavior, for example one piece of code can redefine a #define made by another. This can be hard to track down.

    0 讨论(0)
  • 2020-11-30 01:11

    It's always better to use an enum if possible. Using an enum gives the compiler more information about your source code, a preprocessor define is never seen by the compiler and thus carries less information.

    For implementing e.g. a bunch of modes, using an enum makes it possible for the compiler to catch missing case-statements in a switch, for instance.

    0 讨论(0)
  • 2020-11-30 01:13

    Enum:

    1. Generally used for multiple values

    2. In enum there are two thing one is name and another is value of name name must be distinguished but value can be same.If we not define value then first value of enum name is 0 second value is 1,and so on, unless explicitly value are specified.

    3. They may have type and compiler can type check them

    4. Make debugging easy

    5. We can limit scope of it up to a class.

    Define:

    1. When we have to define only one value

    2. It generally replace one string to another string.

    3. It scope is global we cannot limit its scope

    Overall we have to use enum

    0 讨论(0)
  • 2020-11-30 01:14

    Another advantage of an enum over a list of defines is that compilers (gcc at least) can generate a warning when not all values are checked in a switch statement. For example:

    enum {
        STATE_ONE,
        STATE_TWO,
        STATE_THREE
    };
    
    ...
    
    switch (state) {
    case STATE_ONE:
        handle_state_one();
        break;
    case STATE_TWO:
        handle_state_two();
        break;
    };
    

    In the previous code, the compiler is able to generate a warning that not all values of the enum are handled in the switch. If the states were done as #define's, this would not be the case.

    0 讨论(0)
  • 2020-11-30 01:15

    For integral constant values I've come to prefer enum over #define. There seem to be no disadvantages to using enum (discounting the miniscule disadvantage of a bit more typing), but you have the advantage that enum can be scoped, while #define identifiers have global scope that tromps everything.

    Using #define isn't usually a problem, but since there are no drawbacks to enum, I go with that.

    In C++ I also generally prefer enum to const int even though in C++ a const int can be used in place of a literal integer value (unlike in C) because enum is portable to C (which I still work in a lot) .

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