C++ 命名空间中的静态变量与非静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11623451/
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 vs non-static variables in namespace
提问by Michael Dorst
I have a namespace foo
which contains an integer bar
, declared so...
我有一个foo
包含 integer的命名空间bar
,声明为...
foo.h:
foo.h:
namespace foo {
int bar;
}
Now if I include foo.h
in only one file, this works just fine. But a problem arises when I include foo.h
from two or more files: I get a linker error. I figured out that if I declare bar
as static
, I can include foo.h
in more than one file. This seems strange to me, because I wasn't aware that one could declare a static variable inside of a namespace. (what does that even mean?)
现在,如果我foo.h
只包含在一个文件中,这工作得很好。但是当我foo.h
从两个或多个文件中包含时会出现问题:我收到链接器错误。我发现如果我声明bar
为static
,我可以包含foo.h
在多个文件中。这对我来说似乎很奇怪,因为我不知道可以在命名空间内声明一个静态变量。(那有什么意思?)
Why does this work? And more importantly, why doesn'tit work withoutstatic
? What does static
mean when used in a namespace
?
为什么这样做?更重要的是,为什么没有它的工作没有static
?static
在 a 中使用时是什么意思namespace
?
回答by David Rodríguez - dribeas
There are multiple meanings for static
in different contexts. In this particular context it means that the variable has internal linkage, and thus each translation unit that includes that header will have it's own copy of the variable.
static
在不同的上下文中有多种含义。在这个特定的上下文中,这意味着变量具有内部链接,因此包含该标题的每个翻译单元都将拥有自己的变量副本。
Note that while this will silent the linker error, it will do so maintaining a separate foo::bar
variable for each one of the object files generated (changes will not be visible across different object files).
请注意,虽然这将使链接器错误静默,但它会foo::bar
为每个生成的目标文件维护一个单独的变量(更改将在不同的目标文件中不可见)。
If you want a single variable, you should declare it as extern
in the header and provide a single definition in one translation unit.
如果您需要单个变量,则应extern
在标题中声明它并在一个翻译单元中提供单个定义。
回答by iammilind
When you declare a variable as static
, it means that its scope is limited to the given translation unitonly. Without static
the scope is global.
当您将变量声明为 时static
,这意味着其范围仅限于给定的翻译单元。没有static
范围是全局的。
When you declare a variable as static
inside a .h file (within or without namespace
; doesn't matter), and include that header file in various .cpp files, the static
variable becomes locally scoped to each of the .cpp
files.
So now, every .cpp file that includes that header will have its own copy of that variable.
当您static
在 .h 文件中声明一个变量(在或没有namespace
; 无关紧要)并将该头文件包含在各种 .cpp 文件中时,该static
变量将成为每个.cpp
文件的本地范围。
所以现在,每个包含该标头的 .cpp 文件都将拥有该变量的自己的副本。
Without the static
keyword the compiler will generate only one copy of that variable, so as soon as you include the header file in multiple .cpp files the linker will complain about multiple definitions.
如果没有static
关键字,编译器将只生成该变量的一个副本,因此只要将头文件包含在多个 .cpp 文件中,链接器就会抱怨多个定义。
回答by bames53
The problem is caused by having more than one definition of the variable. The definitions in different translation units conflict with each other, just like multiple non-inline function definitions wouldn't work.
该问题是由变量的多个定义引起的。不同翻译单元中的定义相互冲突,就像多个非内联函数定义不起作用一样。
When you make the variable static you're giving the variable internal linkage, so each translation unit has its own independent copy.
当您将变量设为静态时,您将给变量内部链接,因此每个翻译单元都有自己独立的副本。
What you probably actually want is to put only the declaration in a header (using extern) and then put the definition in an implementation file.
您可能真正想要的是仅将声明放在标题中(使用 extern),然后将定义放在实现文件中。