static const vs #define

前端 未结 11 871
醉话见心
醉话见心 2020-11-22 13:01

Is it better to use static const vars than #define preprocessor? Or maybe it depends on the context?

What are advantages/disadvantages for

相关标签:
11条回答
  • 2020-11-22 13:10

    #define can lead to unexpected results:

    #include <iostream>
    
    #define x 500
    #define y x + 5
    
    int z = y * 2;
    
    int main()
    {
        std::cout << "y is " << y;
        std::cout << "\nz is " << z;
    }
    

    Outputs an incorrect result:

    y is 505
    z is 510
    

    However, if you replace this with constants:

    #include <iostream>
    
    const int x = 500;
    const int y = x + 5;
    
    int z = y * 2;
    
    int main()
    {
        std::cout << "y is " << y;
        std::cout << "\nz is " << z;
    }
    

    It outputs the correct result:

    y is 505
    z is 1010
    

    This is because #define simply replaces the text. Because doing this can seriously mess up order of operations, I would recommend using a constant variable instead.

    0 讨论(0)
  • 2020-11-22 13:14
    • A static const is typed (it has a type) and can be checked by the compiler for validity, redefinition etc.
    • a #define can be redifined undefined whatever.

    Usually you should prefer static consts. It has no disadvantage. The prprocessor should mainly be used for conditional compilation (and sometimes for really dirty trics maybe).

    0 讨论(0)
  • 2020-11-22 13:16

    Defining constants by using preprocessor directive #define is not recommended to apply not only in C++, but also in C. These constants will not have the type. Even in C was proposed to use const for constants.

    0 讨论(0)
  • 2020-11-22 13:16

    If you are defining a constant to be shared among all the instances of the class, use static const. If the constant is specific to each instance, just use const (but note that all constructors of the class must initialize this const member variable in the initialization list).

    0 讨论(0)
  • 2020-11-22 13:23

    Personally, I loathe the preprocessor, so I'd always go with const.

    The main advantage to a #define is that it requires no memory to store in your program, as it is really just replacing some text with a literal value. It also has the advantage that it has no type, so it can be used for any integer value without generating warnings.

    Advantages of "const"s are that they can be scoped, and they can be used in situations where a pointer to an object needs to be passed.

    I don't know exactly what you are getting at with the "static" part though. If you are declaring globally, I'd put it in an anonymous namespace instead of using static. For example

    namespace {
       unsigned const seconds_per_minute = 60;
    };
    
    int main (int argc; char *argv[]) {
    ...
    }
    
    0 讨论(0)
  • 2020-11-22 13:23

    If this is a C++ question and it mentions #define as an alternative, then it is about "global" (i.e. file-scope) constants, not about class members. When it comes to such constants in C++ static const is redundant. In C++ const have internal linkage by default and there's no point in declaring them static. So it is really about const vs. #define.

    And, finally, in C++ const is preferable. At least because such constants are typed and scoped. There are simply no reasons to prefer #define over const, aside from few exceptions.

    String constants, BTW, are one example of such an exception. With #defined string constants one can use compile-time concatenation feature of C/C++ compilers, as in

    #define OUT_NAME "output"
    #define LOG_EXT ".log"
    #define TEXT_EXT ".txt"
    
    const char *const log_file_name = OUT_NAME LOG_EXT;
    const char *const text_file_name = OUT_NAME TEXT_EXT;
    

    P.S. Again, just in case, when someone mentions static const as an alternative to #define, it usually means that they are talking about C, not about C++. I wonder whether this question is tagged properly...

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