static keyword in h file and internal linkage

前端 未结 1 2091
离开以前
离开以前 2020-12-15 08:26

Yet another static question. I have read the following:

  • What are static variables?
  • file scope and static floats
  • http://msdn.mic
1条回答
  •  無奈伤痛
    2020-12-15 08:32

    If StaticTest.h is shared between difference source files then you will get undefined behaviour.

    If you define a class or inline functions in different translation units then their definitions must be the same (same sequence of tokens) and, crucially, any identifiers must refer to the same entity (unless a const object with internal linkage) as in the definition in another translation unit.

    You violate this because counter has internal linkage so in different translation units the identifier in the function definitions refers to a different object.

    Reference: C++03 3.2 [basic.def.odr] / 5.

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