在头文件中包含静态全局变量是否有意义?

发布于 2024-10-18 04:23:51 字数 422 浏览 7 评论 0原文

静态变量具有文件范围。假设我有以下两个文件:

  • file1.h
  • file1.cpp
  • file2.h
  • file2.cpp

我在两个头文件中都声明了静态变量 static int Var1file1.hfile2.h 都包含在 main.cpp 文件中。

我这样做是因为静态变量将具有文件范围,因此不会相互冲突。 但编译后发现有冲突。

现在静态变量的行为就像外部变量一样。另一方面,如果我在两个 .cpp 文件中声明静态变量,则它可以很好地编译。

我无法理解这种行为。

任何人都可以解释一下范围和链接在这种情况下是如何工作的吗?

Static variable has file scope. Say I have two following files:

  • file1.h
  • file1.cpp
  • file2.h
  • file2.cpp

I have declared static variable say static int Var1 in both the header files. Both file1.h and file2.h are included in main.cpp file.

I did this since the static variable will have file scope so it won't conflict each other.
But after compilation I found it is showing conflict.

Now static variable is behaving like a extern variable. On the other hand if I declare the static variable in both .cpp files, it compiles well.

I am unable to understand this behavior.

Can any body explain how scope and linkage are working in this scenario.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

迷途知返 2024-10-25 04:23:51

静态变量是编译单元的本地变量。 编译单元基本上是一个.cpp文件,其中插入了.h文件的内容来代替每个#include指令。

现在,在编译单元中不能有两个同名的全局变量。这就是您的情况所发生的情况: main.cpp 包含 file1.hfile.h,并且这两个标头中的每一个都定义了自己的Var1

如果逻辑上这是两个不同的变量,请给它们不同的名称(或将它们放在不同的命名空间中)。

如果它们是相同的变量,请将其移至单独的头文件 var1.h 中,并包含 file1.h 中的 var1.hfile2.h,不要忘记 中的 #includeguard >var1.h

Static variables are local to the compilation unit. A compilation unit is basically a .cpp file with the contents of the .h file inserted in place of each #include directive.

Now, in a compilation unit you can't have two global variables with the same name. This is what's happening in your case: main.cpp includes file1.h and file.h, and each of the two headers defines its own Var1.

If logically these are two distinct variables, give them different names (or put them in different namespaces).

If these are the same variable, move it into a separate header file, var1.h, and include var1.h from both file1.h and file2.h, not forgetting the #include guard in var1.h.

梦幻的心爱 2024-10-25 04:23:51

静态变量具有翻译单元范围(通常是 .c.cpp 文件),但 #include 指令只是复制文件的文本逐字翻译,并且不会创建另一个翻译单元。经过预处理后,这样:

#include "file1.h"
#include "file2.h"

将变成这样:

/* file1.h contents */
static int Var1;

/* file2.h contents */
static int Var1;

正如你所知,这是无效的。

Static variables have translation unit scope (usually a .c or .cpp file), but an #include directive simply copies the text of a file verbatim, and does not create another translation unit. After preprocessing, this:

#include "file1.h"
#include "file2.h"

Will turn into this:

/* file1.h contents */
static int Var1;

/* file2.h contents */
static int Var1;

Which, as you know, is invalid.

汹涌人海 2024-10-25 04:23:51

假设静态变量 static int Var1 位于两个标头中的全局范围,并且将两个标头都包含在 main.cpp 中。现在,预处理器首先将包含文件的内容复制到 main.cpp 中。由于在 main.cpp 中,在同一范围内声明了两次 Var1,因此会出现多重声明错误。 (即,一个从 file1.h 复制,另一个由预处理器从 file2.h 复制)

每个源文件都是单独编译的。现在,当您在源文件中单独声明时,每个源文件都不知道另一个同名源文件中存在另一个静态变量。所以,编译器不会报告错误。如果您希望在源文件之间共享变量,可以将其标记为 extern。

Assuming static variable static int Var1 is at global scope in both the headers and included both the headers in main.cpp. Now, first the pre-processor copies the content of included files to the main.cpp. Since, at main.cpp there is Var1 declared twice at the same scope, multiple declaration error will arise. ( i.e, one copied from file1.h and the other form file2.h by the pre-processor)

Each source file is compiled individually. Now, when you declare seperately in their source files, each source file is unaware of existence of the other static variable present in the other source file bearing the same name. So, compiler don't report an error. You can mark it as extern, if you want a variable to be shared among the source files.

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