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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:21:59  来源:igfitidea点击:

static vs non-static variables in namespace

c++staticnamespaces

提问by Michael Dorst

I have a namespace foowhich contains an integer bar, declared so...

我有一个foo包含 integer的命名空间bar,声明为...

foo.h:

foo.h:

namespace foo {
    int bar;
}

Now if I include foo.hin only one file, this works just fine. But a problem arises when I include foo.hfrom two or more files: I get a linker error. I figured out that if I declare baras static, I can include foo.hin 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从两个或多个文件中包含时会出现问题:我收到链接器错误。我发现如果我声明barstatic,我可以包含foo.h在多个文件中。这对我来说似乎很奇怪,因为我不知道可以在命名空间内声明一个静态变量。(那有什么意思?)

Why does this work? And more importantly, why doesn'tit work withoutstatic? What does staticmean when used in a namespace?

为什么这样做?更重要的是,为什么没有它的工作没有staticstatic在 a 中使用时是什么意思namespace

回答by David Rodríguez - dribeas

There are multiple meanings for staticin 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::barvariable 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 externin 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 staticthe scope is global.

当您将变量声明为 时static,这意味着其范围仅限于给定的翻译单元。没有static范围是全局的。

When you declare a variable as staticinside a .h file (within or without namespace; doesn't matter), and include that header file in various .cpp files, the staticvariable becomes locally scoped to each of the .cppfiles.
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 statickeyword 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),然后将定义放在实现文件中。