What is the difference between a const variable and a static variable and which should I choose?

前端 未结 3 1997
借酒劲吻你
借酒劲吻你 2020-12-03 21:46

I know this from RFC 246:

  • constants declare constant values. These represent a value, not a memory address. This is
3条回答
  •  囚心锁ツ
    2020-12-03 22:24

    There's not much practical difference if your variable isn't intended to change.

    Constants are inlined at compilation, which means they're copied to every location they're used, and thus are usually more efficient, while statics refer to a unique location in memory and are more like global variables.

    Constants are... constant while statics, while still global, can be mutable.

提交回复
热议问题