C++ 头文件中的静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5040525/
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
Static variable in a Header File
提问by Chris_vr
Static variable has file scope. Say I have two following files:
静态变量具有文件作用域。假设我有以下两个文件:
- file1.h
- file1.cpp
- file2.h
- file2.cpp
- 文件1.h
- 文件1.cpp
- 文件2.h
- 文件2.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.
我static int Var1
在两个头文件中都声明了静态变量 say 。双方file1.h
并file2.h
都包含在main.cpp
文件中。
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.
现在静态变量的行为就像一个extern
变量。另一方面,如果我在两个 .cpp 文件中声明静态变量,它编译良好。
I am unable to understand this behavior.
我无法理解这种行为。
Can any body explain how scope and linkage are working in this scenario.
任何机构都可以解释范围和链接在这种情况下是如何工作的。
回答by NPE
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.
静态变量是编译单元的本地变量。编译单元基本上是一个.cpp
文件,其中.h
插入了文件内容来代替每个#include
指令。
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
.
现在,在编译单元中不能有两个同名的全局变量。这就是您的情况发生的情况:main.cpp
包括file1.h
和file.h
,并且两个标头中的每一个都定义了自己的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 guardin var1.h
.
如果这些是相同的变量,将其移动到一个单独的头文件,var1.h
和包括var1.h
来自两个file1.h
和file2.h
,不要忘记的#include后卫在var1.h
。
回答by moatPylon
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:
静态变量具有翻译单元范围(通常是 a.c
或.cpp
文件),但#include
指令只是逐字复制文件的文本,而不会创建另一个翻译单元。预处理后,这个:
#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.
如您所知,这是无效的。
回答by Mahesh
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)
假设静态变量static int Var1
在两个标头中都处于全局范围内,并且在main.cpp
. 现在,首先预处理器将包含文件的内容复制到main.cpp
. 因为,在main.cpp
有Var1
在同一范围内声明的两倍,将出现多个声明错误。(即,从预处理器复制file1.h
的另一种形式file2.h
)
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.
每个源文件都是单独编译的。现在,当您在它们的源文件中单独声明时,每个源文件都不知道存在于另一个同名源文件中的另一个静态变量的存在。所以,编译器不会报错。如果您希望在源文件之间共享变量,则可以将其标记为 extern。