Okay to declare static global variable in .h file?

后端 未结 6 1994
野趣味
野趣味 2020-12-13 07:36

static keyword keeps the scope of a global variable limited to that translation unit. If I use static int x in a .h file and include that .h file every

6条回答
  •  离开以前
    2020-12-13 08:19

    The observable difference for variables that are const qualified is that in the static version you will get one copy per translation unit and so address comparisons of two such copies may fail.

    If you never use the address of your const variable any modern compiler should be able to just use the value and optimize the variable itself out. In such a case a static const-qualified variable is completely fine.

提交回复
热议问题