Why would someone use #define to define constants?

前端 未结 9 1655
星月不相逢
星月不相逢 2020-11-27 04:02

It\'s simple question but why would someone use #define to define constants?

What\'s the difference between

#define sum 1 and const int su

相关标签:
9条回答
  • 2020-11-27 04:11

    For the example that you just gave, I would normally use a const. Except of course, the #define can be used for conditional compilation elsewhere:

    #if SOME_DEFINE == 1
        // Conditional code
    #endif
    

    This is something you can't do with a const. If you don't need the value to be accessible from the preprocessor, I'd say use a const unless there's some reason why that's not possible. There's some stuff on this in the C++ FAQ lite, where they rightly point out that just because the preprocessor is "evil", it doesn't mean you'll never need it.

    0 讨论(0)
  • 2020-11-27 04:14

    No one should not!
    Actually, One should prefer const int sum = 1; over #define sum 1 for a number of reasons:

    Scope Based Mechanism:

    #defines don't respect scopes so there is no way to create a class scoped namespace. While const variables can be scoped in classes.


    Avoiding Weird magical numbers during compilation errors:

    If you are using #define those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.


    Ease of Debugging:

    Also for same reasons mentioned in #2, while debugging #define would provide no help really.


    Thus, to avoid the above situations const will be a better choice.

    0 讨论(0)
  • 2020-11-27 04:15

    #define has many different applications, but your question seems to be about one specific application: defining named constants.

    In C++ there's rarely a reason to use #define to define named constants.

    #define is normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short, const int objects are not constants in C, which means that in C the primary way to define a true constant is to use #define. (Also, for int constants one can use enums).

    0 讨论(0)
  • 2020-11-27 04:15

    From Introduction to programming with C++ which was written by Daniel Liang stated that:

    When you define a constant using #define directive, the constant is not stored in memory.The constant will be replaced with a value by compiler. When you declare constant using const keyword, the constant is stored in memory just like variable.

    If constant need to be used in multiple programs, use #define to define it in header file so it can be included in other program. If constant used only in one program, using const to declare is more efficient.

    0 讨论(0)
  • 2020-11-27 04:23

    In simple words

    #define sum 1  /*is pre processed*/
    

    Which means, that sum doesn't exist after the preprocessing stage is finished.

    const int sum = 1; /*is compiled/linked*/
    

    Which means sum exists till the executable is made out of your program.

    0 讨论(0)
  • 2020-11-27 04:27

    Always try to use "const int", rather than #define.

    Use #define, only when your preprocessor code might be read by another tool, and it's easier for it to go with the preprocessor, rather than to parse the language.

    Also it's the only way to define something to be checked later by #if/#else/#endif

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