C++ 全局变量声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19929681/
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
C++ Global variable declaration
提问by Shahriyar
What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header
Here is how I tried :
我想要做的只是在头文件中定义一个变量并在两个不同的 cpp 文件中使用它,而无需在每次包含该头文件时重新定义该变量
这是我尝试的方法:
Variables.h
变量.h
#ifndef VARIABLES_H // header guards
#define VARIABLES_H
static bool bShouldRegister;
#endif
(I also tried extern but nothing changed)
(我也试过 extern 但没有任何改变)
And in a cpp file I give it a value ::bShouldRegister = true
or bShouldRegister = true;
在一个 cpp 文件中,我给它一个值::bShouldRegister = true
或bShouldRegister = true;
In my another cpp file I check it's value by creating a thread and check its value in a loop (and yes my thread function works well)
在我的另一个 cpp 文件中,我通过创建一个线程并在循环中检查它的值来检查它的值(是的,我的线程功能运行良好)
while (true)
{
if (::bShouldRegister) // Or if (bShouldRegister)
{
MessageBox(NULL,"Value Changed","Done",MB_OK|MB_ICONINFORMATION);
}
Sleep(100);
}
And yes, that MessageBox never appears (bShouldRegister never gets true :/)
是的,MessageBox 永远不会出现(bShouldRegister 永远不会成为真:/)
回答by masoud
You must use extern
, otherwise you have separate bShouldRegister
in each translation unit with probably different values.
您必须使用extern
,否则您将bShouldRegister
在每个翻译单元中使用可能不同的值进行分隔。
Put this in a header file (.h):
将其放在头文件 (.h) 中:
extern bool bShouldRegister;
Put this just in one of implementation files (.cpp):
把它放在一个实现文件 (.cpp) 中:
bool bShouldRegister;
回答by Wolf
A more C++-like waywould be using a class member, syntactically indicated by the static
keyword. Class member variables have implicit external
linkage.
更像C++ 的方法是使用类成员,在语法上由static
关键字指示。类成员变量具有隐式external
链接。
#ifndef VARIABLES_H
#define VARIABLES_H
class RegUtil {
public:
static bool bShouldRegister;
};
#endif
in one of your cpp files (maybe variables.cpp
), you have to define this class member:
在您的一个 cpp 文件(也许variables.cpp
)中,您必须定义此类成员:
#include "variables.h"
bool RegUtil::bShouldRegister;
回答by bcd
If you can use C++17, consider using an inline variable:
如果您可以使用 C++17,请考虑使用内联变量:
// in a header file
inline bool bShouldRegister = true;
See How do inline variables work?for more information.
请参阅内联变量如何工作?想要查询更多的信息。
回答by Fred Foo
You need to define the variable in one of the modules:
您需要在以下模块之一中定义变量:
bool bShouldRegister;
Then declare it extern
(not static
!) in the header:
然后在标题中声明它extern
(不是static
!):
extern bool bShouldRegister;
回答by Jayesh Vaghasiya
Here you need to define bool bShouldRegister in one class. Your header file should like this.
这里需要在一个类中定义 bool bShouldRegister。你的头文件应该是这样的。
#ifndef VARIABLES_H // header guards
#define VARIABLES_H
class abc{
public:
bool bShouldRegister;
abc();
#endif
Now initialize bShouldRegistervariable in cpp file in constructor of abc class and then use this variable in your second cpp file using object of class. You will get your message box.
现在在 abc 类的构造函数中的 cpp 文件中初始化bShouldRegister变量,然后使用类的对象在第二个 cpp 文件中使用此变量。你会得到你的消息框。