C++中的静态变量

Static variables in C++(C++中的静态变量)

本文介绍了C++中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道头文件中的静态变量与类中声明的静态变量有什么区别.在头文件中声明静态变量时,其范围仅限于 .h 文件或跨所有单元.在类中声明时,通常静态变量也在 .cpp 文件中初始化,对吗?那么这是否意味着静态变量范围仅限于 2 个编译单元?

I would like to know what is the difference between static variables in a header file vs declared in a class. When static variable is declared in a header file is its scope limited to .h file or across all units. Also generally static variable is initialized in .cpp file when declared in a class right? So that does mean static variable scope is limited to 2 compilation units?

推荐答案

请原谅我乱序回答你的问题,这样更容易理解.

Excuse me when I answer your questions out-of-order, it makes it easier to understand this way.

在头文件中声明静态变量时,其作用域仅限于 .h 文件或跨所有单元.

When static variable is declared in a header file is its scope limited to .h file or across all units.

没有头文件范围"这样的东西.头文件被包含到源文件中.翻译单元是源文件包括头文件中的文本.您在头文件中写入的任何内容都会复制到每个包含的源文件中.

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.

由于以这种方式声明一个变量 static 意味着内部链接,每个翻译单元 #include 在你的头文件中都有它的自己的individual 变量(在您的翻译单元之外不可见).这通常不是您想要的.

Since declaring a variable static this way means internal linkage, every translation unit #includeing your header file gets its own, individual variable (which is not visible outside your translation unit). This is usually not what you want.

我想知道头文件中的静态变量与类中声明的静态变量有什么区别.

I would like to know what is the difference between static variables in a header file vs declared in a class.

在类声明中,static 表示类的所有实例共享这个成员变量;即,您可能有数百个这种类型的对象,但是每当这些对象之一引用 static(或类")变量时,它对于所有对象都是相同的值.您可以将其视为全局类".

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 the static (or "class") variable, it's the same value for all objects. You could think of it as a "class global".

通常静态变量在类中声明时也在.cpp文件中初始化,对吗?

Also generally static variable is initialized in .cpp file when declared in a class right ?

是的,一个(并且只有一个)翻译单元必须初始化类变量.

Yes, one (and only one) translation unit must initialize the class variable.

这是否意味着静态变量范围仅限于 2 个编译单元?

So that does mean static variable scope is limited to 2 compilation units ?

正如我所说:

  • 标头不是编译单元,
  • static 根据上下文意味着完全不同的东西.
  • A header is not a compilation unit,
  • static means completely different things depending on context.

Global static 将范围限制为翻译单元.类 static 表示对所有实例全局.

Global static limits scope to the translation unit. Class static means global to all instances.

我希望这会有所帮助.

PS: 检查 Chubsdad 答案的最后一段,关于如何在 C++ 中使用 static 来指示内部链接,而是使用匿名命名空间.(因为他是对的.;-))

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. ;-) )

这篇关于C++中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++中的静态变量

基础教程推荐