简体   繁体   English

C ++中的静态变量

[英]Static variables in C++

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. 在头文件中声明静态变量时,其范围仅限于.h文件或所有单元。 Also generally static variable is initialized in .cpp file when declared in a class right? 一般来说静态变量在类中声明时在.cpp文件中初始化了吗? So that does mean static variable scope is limited to 2 compilation units? 那么这是否意味着静态变量范围仅限于2个编译单元?

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 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). 由于这种方式声明变量static意味着内部链接,每个翻译单元#include你的头文件都有自己的 单个变量(在你的翻译单元之外是不可见的)。 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. 我想知道头文件中的静态变量与类中声明的静态变量之间的区别。

In a class declaration, static means that all instances of the class share this member variable; 在类声明中, static表示该类的所有实例共享此成员变量; ie, 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. 也就是说,您可能有数百个此类型的对象,但只要其中一个对象引用static (或“类”)变量,它就是所有对象的相同值。 You could think of it as a "class global". 您可以将其视为“全球级”。

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, 标头不是编译单元,
  • static means completely different things depending on context. static意味着根据上下文完全不同的东西。

Global static limits scope to the translation unit. 全局static限制了翻译单元的范围。 Class static means global to all instances. static意味着全局到所有实例。

I hope this helps. 我希望这有帮助。

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. PS:检查Chubsdad的答案的最后一段,关于如何在C ++中使用static来指示内部链接,但是匿名命名空间。 (Because he's right. ;-) ) (因为他是对的。;-))

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). 此变量'zzz'具有内部链接(在其他翻译单元中无法访问此相同变量)。 Each translation unit which includes 'common.h' has it's own unique object of name 'zzz' . 每个包含'common.h'翻译单元都有自己独特的名称'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.cpp has 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. 因此,类静态成员不仅限于2个翻译单元。 They need to be defined only once in any one of the translation units. 它们只需在任何一个翻译单元中定义一次。

Note: usage of 'static' to declare file scope variable is deprecated and unnamed namespace is a superior alternate 注意:不推荐使用'static'来声明文件范围变量,并且未命名的命名空间是一种优越的替代方法

A static variable declared in a header file outside of the class would be file-scoped in every .c file which includes the header. 在类外部的头文件中声明的静态变量将在包含头的每个.c文件中进行file-scoped That means separate copy of a variable with same name is accessible in each of the .c files where you include the header file. 这意味着在包含头文件的每个.c文件中都可以访问具有相同名称的变量的单独副本。

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. 另一方面,静态类变量是class-scoped并且每个编译单元都可以使用相同的静态变量,其中包含包含具有静态变量的类的标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM