What is the difference between static const and const?

后端 未结 4 585
借酒劲吻你
借酒劲吻你 2020-11-30 20:21

What is the difference between static const and const? For example:

static const int a=5;
const int i=5;

Is there

4条回答
  •  臣服心动
    2020-11-30 20:54

    The difference is the linkage.

    // At file scope
    static const int a=5;  // internal linkage
    const int i=5;         // external linkage
    

    If the i object is not used outside the translation unit where it is defined, you should declare it with the static specifier.

    This enables the compiler to (potentially) perform further optimizations and informs the reader that the object is not used outside its translation unit.

提交回复
热议问题