Difference between Enum and Define Statements

后端 未结 18 2478
终归单人心
终归单人心 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:58

    Besides all the thing already written, one said but not shown and is instead interesting. E.g.

    enum action { DO_JUMP, DO_TURNL, DO_TURNR, DO_STOP };
    //...
    void do_action( enum action anAction, info_t x );
    

    Considering action as a type makes thing clearer. Using define, you would have written

    void do_action(int anAction, info_t x);
    
    0 讨论(0)
  • 2020-11-30 00:59

    #define statements are handled by the pre-processor before the compiler gets to see the code so it's basically a text substitution (it's actually a little more intelligent with the use of parameters and such).

    Enumerations are part of the C language itself and have the following advantages.

    1/ They may have type and the compiler can type-check them.

    2/ Since they are available to the compiler, symbol information on them can be passed through to the debugger, making debugging easier.

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

    Define is a preprocessor command, it's just like doing "replace all" in your editor, it can replace a string with another and then compile the result.

    Enum is a special case of type, for example, if you write:

    enum ERROR_TYPES
    {
       REGULAR_ERR =1,
       OK =0
    }
    

    there exists a new type called ERROR_TYPES. It is true that REGULAR_ERR yields to 1 but casting from this type to int should produce a casting warning (if you configure your compiler to high verbosity).

    Summary: they are both alike, but when using enum you profit the type checking and by using defines you simply replace code strings.

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

    Enums are generally prefered over #define wherever it makes sense to use an enum:

    • Debuggers can show you the symbolic name of an enums value ("openType: OpenExisting", rather than "openType: 2"
    • You get a bit more protection from name clashes, but this isn't as bad as it was (most compilers warn about re#defineition.

    The biggest difference is that you can use enums as types:

    // Yeah, dumb example
    enum OpenType {
        OpenExisting,
        OpenOrCreate,
        Truncate
    };
    
    void OpenFile(const char* filename, OpenType openType, int bufferSize);
    

    This gives you type-checking of parameters (you can't mix up openType and bufferSize as easily), and makes it easy to find what values are valid, making your interfaces much easier to use. Some IDEs can even give you intellisense code completion!

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

    If you have a group of constants (like "Days of the Week") enums would be preferable, because it shows that they are grouped; and, as Jason said, they are type-safe. If it's a global constant (like version number), that's more what you'd use a #define for; although this is the subject of a lot of debate.

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

    enum can group multiple elements in one category:

    enum fruits{ apple=1234, orange=12345};
    

    while #define can only create unrelated constants:

    #define apple 1234
    #define orange 12345
    
    0 讨论(0)
提交回复
热议问题