在头文件中包含静态全局变量是否有意义?
静态变量具有文件范围。假设我有以下两个文件:
- file1.h
- file1.cpp
- file2.h
- file2.cpp
我在两个头文件中都声明了静态变量 static int Var1
。 file1.h
和 file2.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态变量是编译单元的本地变量。 编译单元基本上是一个
.cpp
文件,其中插入了.h
文件的内容来代替每个#include指令。
现在,在编译单元中不能有两个同名的全局变量。这就是您的情况所发生的情况:
main.cpp
包含file1.h
和file.h
,并且这两个标头中的每一个都定义了自己的Var1
。如果逻辑上这是两个不同的变量,请给它们不同的名称(或将它们放在不同的命名空间中)。
如果它们是相同的变量,请将其移至单独的头文件
var1.h
中,并包含file1.h
中的var1.h
和file2.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
includesfile1.h
andfile.h
, and each of the two headers defines its ownVar1
.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 includevar1.h
from bothfile1.h
andfile2.h
, not forgetting the #include guard invar1.h
.静态变量具有翻译单元范围(通常是
.c
或.cpp
文件),但#include
指令只是复制文件的文本逐字翻译,并且不会创建另一个翻译单元。经过预处理后,这样:将变成这样:
正如你所知,这是无效的。
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:Will turn into this:
Which, as you know, is invalid.
假设静态变量
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 inmain.cpp
. Now, first the pre-processor copies the content of included files to themain.cpp
. Since, atmain.cpp
there isVar1
declared twice at the same scope, multiple declaration error will arise. ( i.e, one copied fromfile1.h
and the other formfile2.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.