C++中的静态变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3698043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:30:20  来源:igfitidea点击:

Static variables in C++

c++staticscope

提问by brett

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?

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

回答by DevSolar

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

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

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

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

There is no such thing as a "header file scope". The header file gets includedinto source files. The translation unit is the source file includingthe text from the header files. Whatever you write in a header file gets copiedinto 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 staticthis way means internal linkage, every translation unit #includeing your header file gets its own, individualvariable (which is not visible outside your translation unit). This is usually not what you want.

由于声明一个变量static这种方式意味着内部连接,每个翻译单元#include荷兰国际集团的头文件中获取自己的单独的变量(不可见之外的翻译单元)。这通常不是您想要的。

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

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

In a class declaration, staticmeans that all instances of the class sharethis 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".

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

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

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

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

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

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

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

As I said:

就像我说的:

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

Global staticlimits scope to the translation unit. Class staticmeans global to all instances.

全局static限制翻译单元的范围。类static意味着对所有实例都是全局的。

I hope this helps.

我希望这有帮助。

PS:Check the last paragraph of Chubsdad's answer, about how you shouldn't use staticin C++ for indicating internal linkage, but anonymous namespaces. (Because he's right. ;-) )

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

回答by Chubsdad

Static variable in a header file:

头文件中的静态变量:

say 'common.h'has

'common.h'

static int zzz;

This 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'.

此变量'zzz'具有内部链接(在其他翻译单元中无法访问同一变量)。包含的每个翻译单元'common.h'都有自己独特的 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.

类中的静态变量不是该类子对象的一部分。只有一个静态数据成员的副本被类的所有对象共享。

$9.4.2/6 - "Static data members of a class in namespace scope have external linkage (3.5).A local class shall not have static data members."

$9.4.2/6 - “命名空间范围内类的静态数据成员具有外部链接(3.5)。本地类不应具有静态数据成员。”

So let's say 'myclass.h'has

所以让我们说'myclass.h'

struct myclass{
   static int zzz;        // this is only a declaration
};

and myclass.cpphas

并且myclass.cpp

#include "myclass.h"

int myclass::zzz = 0           // this is a definition, 
                               // should be done once and only once

and "hisclass.cpp"has

并且"hisclass.cpp"

#include "myclass.h"

void f(){myclass::zzz = 2;}    // myclass::zzz is always the same in any 
                               // translation unit

and "ourclass.cpp"has

并且"ourclass.cpp"

#include "myclass.h"
void g(){myclass::zzz = 2;}    // myclass::zzz is always the same in any 
                               // translation unit

So, 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.

因此,类静态成员不仅限于 2 个翻译单元。它们只需要在任何一个翻译单元中定义一次。

Note: usage of 'static' to declare file scope variable is deprecated and unnamed namespace is a superior alternate

注意:不推荐使用“静态”来声明文件范围变量,而未命名的命名空间是一个更好的选择

回答by Goutham

A static variable declared in a header file outside of the class would be file-scopedin 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.

在类外部的头文件中声明的静态变量将位于包含头文件的file-scoped每个 .c 文件中。这意味着可以在包含头文件的每个 .c 文件中访问具有相同名称的变量的单独副本。

A static class variable on the other hand is class-scopedand the same static variable is available to every compilation unit that includes the header containing the class with static variable.

另一方面,静态类变量是class-scoped相同的静态变量,并且每个编译单元都可以使用相同的静态变量,该编译单元包括包含具有静态变量的类的头文件。