在头文件中,静态全局变量和静态数据成员有什么区别?

发布于 2024-09-19 02:06:59 字数 131 浏览 7 评论 0原文

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

当在头文件中声明静态变量时,其范围仅限于 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-09-26 02:07:00

在类外部的头文件中声明的静态变量在包含该头的每个 .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.

往事随风而去 2024-09-26 02:06:59

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

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

不存在“头文件范围”这样的东西。头文件被包含到源文件中。翻译单元是源文件,包括头文件中的文本。无论您在头文件中写入什么内容,都会复制到每个包含源文件中。

因此,头文件中声明的静态变量就像每个单独的源文件中的静态变量。

由于以这种方式声明变量static意味着内部链接,因此头文件中#include的每个翻译单元都会获得其自己的个体< /strong> 变量(在翻译单元外部不可见)。这通常不是您想要的。

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

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

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

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

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

正如我所说:

  • 标头不是编译单元,
  • 静态意味着完全不同的事物,具体取决于上下文。

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

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

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.

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 #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.

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".

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.

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

As I said:

  • A header is not a compilation unit,
  • static means completely different things depending on context.

Global static limits scope to the translation unit. Class static 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. ;-) )

掌心的温暖 2024-09-26 02:06:59

头文件中的静态变量:

'common.h'

static int zzz;

这个变量'zzz'有内部链接(这个相同的变量不能被在其他翻译单元中访问)。每个包含 'common.h' 的翻译单元都有其自己唯一的名为 'zzz' 的对象。

类中的静态变量:

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

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

因此,假设 'myclass.h' 具有

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

myclass.cpp 具有

#include "myclass.h"

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

"hisclass.cpp" 具有

#include "myclass.h"

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

且 < code>"ourclass.cpp" has

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

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

注意:使用“static”来声明
文件范围变量已被弃用并且
未命名的命名空间是高级的
替代

Static variable in a header file:

say 'common.h' has

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

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."

So let's say 'myclass.h' has

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

and myclass.cpp has

#include "myclass.h"

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

and "hisclass.cpp" has

#include "myclass.h"

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

and "ourclass.cpp" has

#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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文