在头文件中,静态全局变量和静态数据成员有什么区别?
我想知道头文件中的静态变量与类中声明的静态变量有什么区别。
当在头文件中声明静态变量时,其范围仅限于 .h 文件或跨所有单元。通常,静态变量在类中声明时会在 .cpp 文件中初始化,对吗?这是否意味着静态变量作用域仅限于两个编译单元?
I would like to know what is the difference between static
variables in a header file vs one declared in a class.
When a static
variable is declared in a header file is its scope limited to .h file or across all units. Generally, a static
variable is initialized in a .cpp file when declared in a class, right? Does that mean a static
variable scope is limited to two compilation units?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在类外部的头文件中声明的静态变量在包含该头的每个 .c 文件中都将是
文件范围
。这意味着在包含头文件的每个 .c 文件中都可以访问具有相同名称的变量的单独副本。另一方面,静态类变量是类范围的,并且相同的静态变量可用于包含包含静态变量的类的标头的每个编译单元。
A static variable declared in a header file outside of the class would be
file-scoped
in every .c file which includes the header. That means separate copy of a variable with same name is accessible in each of the .c files where you include the header file.A static class variable on the other hand is
class-scoped
and the same static variable is available to every compilation unit that includes the header containing the class with static variable.请原谅我乱序回答你的问题,这样更容易理解。
不存在“头文件范围”这样的东西。头文件被包含到源文件中。翻译单元是源文件,包括头文件中的文本。无论您在头文件中写入什么内容,都会复制到每个包含源文件中。
因此,头文件中声明的静态变量就像每个单独的源文件中的静态变量。
由于以这种方式声明变量
static
意味着内部链接,因此头文件中#include
的每个翻译单元都会获得其自己的、个体< /strong> 变量(在翻译单元外部不可见)。这通常不是您想要的。在类声明中,
static
表示该类的所有实例共享该成员变量;即,您可能有数百个这种类型的对象,但是每当这些对象之一引用static
(或“类”)变量时,所有对象的值都相同。您可以将其视为“全局类”。是的,一个(并且只有一个)翻译单元必须初始化类变量。
正如我所说:
全局
static
将范围限制为翻译单元。类static
意味着对所有实例都是全局的。PS:检查 Chubsdad 答案的最后一段,了解如何在 C++ 中不应该使用
static
来指示内部链接,而是使用匿名名称空间。 (因为他是对的。;-))Excuse me when I answer your questions out-of-order, it makes it easier to understand this way.
There is no such thing as a "header file scope". The header file gets included into source files. The translation unit is the source file including the text from the header files. Whatever you write in a header file gets copied into each including source file.
As such, a static variable declared in a header file is like a static variable in each individual source file.
Since declaring a variable
static
this way means internal linkage, every translation unit#include
ing your header file gets its own, individual variable (which is not visible outside your translation unit). This is usually not what you want.In a class declaration,
static
means that all instances of the class share this member variable; i.e., you might have hundreds of objects of this type, but whenever one of these objects refers to thestatic
(or "class") variable, it's the same value for all objects. You could think of it as a "class global".Yes, one (and only one) translation unit must initialize the class variable.
As I said:
static
means completely different things depending on context.Global
static
limits scope to the translation unit. Classstatic
means global to all instances.PS: Check the last paragraph of Chubsdad's answer, about how you shouldn't use
static
in C++ for indicating internal linkage, but anonymous namespaces. (Because he's right. ;-) )头文件中的静态变量:
说
'common.h'
有这个变量
'zzz'
有内部链接(这个相同的变量不能被在其他翻译单元中访问)。每个包含'common.h'
的翻译单元都有其自己唯一的名为'zzz'
的对象。类中的静态变量:
类中的静态变量不是该类的子对象的一部分。类的所有对象共享的静态数据成员只有一份副本。
因此,假设
'myclass.h'
具有且
myclass.cpp
具有且
"hisclass.cpp"
具有且 < code>"ourclass.cpp" has
因此,类静态成员不仅限于 2 个翻译单元,它们只需在任何一个翻译单元中定义一次。
Static variable in a header file:
say
'common.h'
hasThis variable
'zzz'
has internal linkage (This same variable can not be accessed in other translation units). Each translation unit which includes'common.h'
has it's own unique object of name'zzz'
.Static variable in a class:
Static variable in a class is not a part of the subobject of the class. There is only one copy of a static data member shared by all the objects of the class.
So let's say
'myclass.h'
hasand
myclass.cpp
hasand
"hisclass.cpp"
hasand
"ourclass.cpp"
hasSo, class static members are not limited to only 2 translation units. They need to be defined only once in any one of the translation units.