C++ 如何声明一个可以在整个程序中使用的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2029272/
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 that could be used in the entire program
提问by trrrrrrm
I have a variable that I would like to use in all my classes without needing to pass it to the class constructor every time I would like to use it. How would I accomplish this in C++?
我有一个变量,我想在我所有的类中使用它,而无需在每次我想使用它时将它传递给类构造函数。我将如何在 C++ 中实现这一点?
Thanks.
谢谢。
回答by fupsduck
global.h
extern int myVar;
global.cpp
#include "global.h"
int myVar = 0; // initialize
class1.cpp
#include "global.h"
...
class2.cpp
#include "global.h"
...
class3.cpp
#include "global.h"
...
MyVar will be known and usable in every module as a global variable. You do not have to have global.cpp. You could initialize myVar in any of the class .cpp's but I think this is cleaner for larger programs.
MyVar 将在每个模块中作为全局变量已知和使用。您不必拥有 global.cpp。您可以在任何 .cpp 类中初始化 myVar,但我认为这对于较大的程序更清晰。
回答by Emile Cormier
If you're not going to use the Singleton pattern as Lyndsey suggests, then at least use a global function (inside a namespace) to access the variable. This will give you more flexibily in how you manage that global entity.
如果您不打算像 Lyndsey 建议的那样使用单例模式,那么至少使用全局函数(在命名空间内)来访问变量。这将使您更灵活地管理该全局实体。
// mymodule.h
namespace mynamespace // prevents polluting the global namespace
{
extern int getGlobalVariable();
}
// mymodule.cpp
namespace mynamespace
{
int myGlobalVariable = 42;
int getGlobalVariable()
{
return myGlobalVariable;
}
}
回答by Alex Brown
Just declare it outside the class:
只需在类外声明它:
Header file:
头文件:
extern int x;
class A {
int z;
public:
A() : z(x++) {}
};
One source file:
一个源文件:
int x = 0;
回答by Lyndsey Ferguson
While I would like to avoid global variables like the plague as our software cannot be multithreaded effectively due to the high reliance on global variables, I do have some suggestions:
虽然我想避免像瘟疫这样的全局变量,因为我们的软件由于高度依赖全局变量而无法有效地进行多线程处理,但我确实有一些建议:
Use a Singleton. It will allow you to keep the code and access clean. Part of the problem with a global variable is you don't know what code has modified it. You could set the value of global somewhere in your function relying on the hope that no one else will change it, but function your code calls, fooA, changes it and now your code is a) broken, and b) hard to debug.
使用单例。它将允许您保持代码和访问干净。全局变量的部分问题是你不知道是什么代码修改了它。您可以在您的函数中的某处设置 global 的值,希望没有其他人会更改它,但是您的代码调用函数 fooA 会更改它,现在您的代码 a) 损坏,并且 b) 难以调试。
If you have to use a global variable without touching the singleton pattern, look at fupsduck's response.
如果您必须在不触及单例模式的情况下使用全局变量,请查看 fupsduck 的响应。
回答by Paul Nathan
keyword extern
关键词 extern
//file1.cpp
int x = 0;
//file1 continues and ends.
//file2.cpp
extern int x; //this gets tied into file1.cpp's x at link time.
//file2.cpp goes on and ends
回答by rtpg
if you want to declare it in different header files/cpp files, just declare it extern
outside of other files
如果你想在不同的头文件/cpp文件中声明它,只需extern
在其他文件之外声明它
//file1.c
int x=1;
int f(){}
//file2.c
extern int x;
回答by justin
// L.hpp
struct L { static int a; };
// L.cpp
int L::a(0);
回答by Drew Dormann
Declare the variable as extern
in a common header.
extern
在公共头文件中声明变量。
Define it in any source file.
在任何源文件中定义它。
回答by Darshan b
The below solution should be simple enough as per the title "How to declare a global variable that could be used in the entire program " . If you want to use it in a different file then make use of extern keyword.
根据标题“如何声明可以在整个程序中使用的全局变量”,下面的解决方案应该足够简单。如果您想在不同的文件中使用它,请使用 extern 关键字。
Please let me know if there is any issue with the solution
如果解决方案有任何问题,请告诉我
#include<iostream>
using namespace std;
int global = 5;
class base {
public:
int a;
float b;
base (int x) {
a = global;
cout << "base class value =" << a << endl;
}
};
class derived : public base {
public:
int c;
float d;
derived (float x, int y) : base (y)
{
d = global;
cout << "derived class value =" << d << endl;
}
};
int main ()
{
derived d(0,0);
cout << "main finished" << endl;
return 0;
}