C++ 如何在C++中声明一个全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9702053/
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
How to declare a global variable in C++
提问by Marcus Tik
I know one should not use global variables but I have a need for them. I have read that any variable declared outside a function is a global variable. I have done so, but in another *.cpp File that variable could not be found. So it was not realy global. Isn't it so that one has to create a header file GlobalVariabels.h and include that file to any other *cpp file that uses it?
我知道不应该使用全局变量,但我需要它们。我读过在函数外声明的任何变量都是全局变量。我已经这样做了,但是在另一个 *.cpp 文件中找不到该变量。所以它并不是真正的全球性的。是不是必须创建一个头文件 GlobalVariabels.h 并将该文件包含到任何其他使用它的 *cpp 文件中?
回答by Lightness Races in Orbit
I have read that any variable declared outside a function is a global variable. I have done so, but in another *.cpp File that variable could not be found. So it was not realy global.
我读过在函数外声明的任何变量都是全局变量。我已经这样做了,但是在另一个 *.cpp 文件中找不到该变量。所以它并不是真正的全球性的。
According to the concept of scope, your variable isglobal. However, what you've read/understood is overly-simplified.
根据scope的概念,您的变量是全局的。但是,您所阅读/理解的内容过于简单。
Possibility 1
可能性 1
Perhaps you forgot to declarethe variable in the other translation unit (TU). Here's an example:
也许您忘记在另一个翻译单元 (TU) 中声明变量。下面是一个例子:
a.cpp
a.cpp
int x = 5; // declaration and definition of my global variable
b.cpp
b.cpp
// I want to use `x` here, too.
// But I need b.cpp to know that it exists, first:
extern int x; // declaration (not definition)
void foo() {
cout << x; // OK
}
Typically you'd place extern int x;
in a header file that gets included into b.cpp, and also into any other TU that ends up needing to use x
.
通常,您会放置extern int x;
在包含到b.cpp中的头文件中,以及最终需要使用.cpp 的任何其他 TU 中x
。
Possibility 2
可能性2
Additionally, it's possible that the variable has internal linkage, meaning that it's not exposed across translation units. This will be the case by default if the variable is marked const
([C++11: 3.5/3]
):
此外,变量可能具有内部链接,这意味着它不会跨翻译单元公开。如果变量被标记为const
( [C++11: 3.5/3]
) ,则默认情况下就是这种情况:
a.cpp
a.cpp
const int x = 5; // file-`static` by default, because `const`
b.cpp
b.cpp
extern const int x; // says there's a `x` that we can use somewhere...
void foo() {
cout << x; // ... but actually there isn't. So, linker error.
}
You could fix this by applying extern
to the definition, too:
您也可以通过应用extern
到定义来解决这个问题:
a.cpp
a.cpp
extern const int x = 5;
This whole malarky is roughly equivalent to the mess you go through making functions visible/usable across TU boundaries, but with some differences in how you go about it.
这整个错误大致相当于您在跨 TU 边界使函数可见/可用所经历的混乱,但在您如何进行时存在一些差异。
回答by Luchian Grigore
You declare the variable as extern
in a common header:
您extern
在公共标头中声明变量:
//globals.h
extern int x;
And define it in an implementation file.
并在实现文件中定义它。
//globals.cpp
int x = 1337;
You can then include the header everywhere you need access to it.
然后,您可以在需要访问它的任何地方包含标题。
I suggest you also wrap the variable inside a namespace
.
我建议您还将变量包装在namespace
.
回答by JTeagle
In addition to other answers here, if the value is an integral constant, a public enum in a class or struct will work. A variable - constant or otherwise - at the root of a namespace is another option, or a static public member of a class or struct is a third option.
除了此处的其他答案外,如果该值是一个整数常量,则类或结构中的公共枚举将起作用。命名空间根部的变量 - 常量或其他 - 是另一种选择,或者类或结构的静态公共成员是第三种选择。
MyClass::eSomeConst (enum)
MyNamespace::nSomeValue
MyStruct::nSomeValue (static)
回答by Dariusz
Not sure if this is correct in any sense but this seems to work for me.
不确定这在任何意义上是否正确,但这似乎对我有用。
someHeader.h
inline int someVar;
I don't have linking/multiple definition issues and it "just works"... ;- )
我没有链接/多重定义问题,它“正常工作”...... ;-)
It's quite handy for "quick" tests... Try to avoid global vars tho, because every says so... ;- )
这对于“快速”测试非常方便......尽量避免全局变量,因为每个人都这么说......;-)