#defining constants in C++

后端 未结 7 994
轮回少年
轮回少年 2021-02-01 03:50

In various C code, I see constants defined like this:

#define T 100

Whereas in C++ examples, it is almost always:

const int T =         


        
相关标签:
7条回答
  • 2021-02-01 04:10

    There is no requirement that T be stored "in memory" in the second case, unless you do something like take the address of it. This is true of all variables.

    The reason the second one is better is that the first will frequently "disappear" in the pre-processor phase so that the compiler phase never sees it (and hence doesn't give it to you in debug information). But that behaviour is not mandated by the standard, rather it's common practice.

    There's little need to use #define statements any more other than for conditional compilation. Single constants can be done with const, multiple related constants can be done with enum and macros can be replaced with inline functions.

    0 讨论(0)
  • 2021-02-01 04:13

    Preprocessor macros do not respect the scope - it's a simple text substitution - while static const int blah = 1; can be enclosed in a namespace. The compiler will still optimize both cases (unless you take address of that variable) but it's type- and scope-safe.

    0 讨论(0)
  • 2021-02-01 04:15

    Due to the differences between the concepts of constant in C and C++, in C we are basically forced to use #define (or enum) most of the time. const just doesn't work in C in most cases.

    But in C++ there's no such problem, so it is indeed bad practice to rely on #defined constants in C++ (unless you really need a textually-substituted constant for some reason).

    0 讨论(0)
  • 2021-02-01 04:21

    Yes. At the very least, use enums. Both const int and enums will be evaluated at compile-time, so you have the same performance. However, it's much cleaner, will be easier to debug (the debugger will actually know what T is), it's type-safe, and less likely to break in complex expressions.

    0 讨论(0)
  • 2021-02-01 04:27

    Yes. The biggest reason is that preprocessor definitions do not obey the scoping rules of the language, polluting the global namespace, and worse -- they're even replaced in cases like

    x->sameNameAsPreprocessorToken
    

    Since preprocessor definitions are replaced at the textual level, other normal properties of variables do not apply - you can take the address of an int const, but not of a #define'd constant.

    As noted by others, you also typically lose type safety and debugging ability.

    0 讨论(0)
  • 2021-02-01 04:28

    Is it considered bad programming practice to #define constants in C++?

    Yes, because all macros (which are what #defines define) are in a single namespace and they take effect everywhere. Variables, including const-qualified variables, can be encapsulated in classes and namespaces.

    Macros are used in C because in C, a const-qualified variable is not actually a constant, it is just a variable that cannot be modified. A const-qualified variable cannot appear in a constant expression, so it can't be used as an array size, for example.

    In C++, a const-qualified object that is initialized with a constant expression (like const int x = 5 * 2;) is a constant and can be used in a constant expression, so you can and should use them.

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