不理解静态布尔行为
我有一个头文件,其中有一些静态变量供我的所有文件使用。我在那里有一个初始化为 0 的布尔变量 -
//in utility.h
static bool read_mess = false;
如果 --view-read-messages 在命令行参数中,我想将其更改为 true ,以便当我从客户端收到消息时可以执行类似的操作 -
//code from a different file
if(UTILITY_H::read_mess)
std::cout<<"\nMessage successfully received from Client 2: "<<in2;
在main 中,我检查命令行参数并将变量 read_mess 设置为 true -
//this is in a for, where temp is the command line arg[i]
else if(strcmp(temp.c_str(), "--view-read-messages") == 0) {
UTILITY_H::read_mess = true;
}
我可以在 main 中的这一行之后打印 read_mess 的值,它表示它是 true。但是当我检查上面发布的 if 语句中的值是否为 true 时,read_mess 又回到了 false。为什么会发生这种情况?我确信这只是简单的事情,但我似乎无法让它发挥作用。每次执行 UTILITY_H:: 时,utility.h 中的所有变量都会重新初始化吗?如果是这样,为什么?
I have a header file that has some static variables for all of my files to use. I have a boolean variable in there initialized to 0 -
//in utility.h
static bool read_mess = false;
that I want to change to true if --view-read-messages is in the command line arguments so that I can do something like this when I get a message from a client -
//code from a different file
if(UTILITY_H::read_mess)
std::cout<<"\nMessage successfully received from Client 2: "<<in2;
In main, I check for the command line argument and set the variable, read_mess, to true -
//this is in a for, where temp is the command line arg[i]
else if(strcmp(temp.c_str(), "--view-read-messages") == 0) {
UTILITY_H::read_mess = true;
}
I can print the value of read_mess after this line in main and it says that it is true. But when I'm checking if its true in the if statement I posted above, read_mess is back to false. Why does this happen? I'm sure its just something simple, but I can't seem to make it work. Are all of the variables in utility.h reinitialized each time I do UTILITY_H::? And if so, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此上下文中,
static
表示“本地”(对于翻译单元)。您的程序中将有多个read_mess
副本,每个翻译单元一个,这与头文件不同。 (在您的情况下,您很可能将“翻译单元”近似为 .cpp 或 .c 或 .cc 文件)。可能您想要做的是声明一个
extern
变量,或静态
类成员并定义 它仅在一个翻译单元中。在实践中,使用
extern
意味着您要在头文件中编写:但在一个且仅有一个其他不是标头的地方:
static
in this context means "local" (to the translation unit). There will be multiple copies ofread_mess
in your program, one per translation unit which is not the same thing as a header file. (In your case you can most likely approximate "translation unit" as .cpp or .c or .cc file).Probably what you meant to do was to declare an
extern
variable, orstatic
class member and define it in just one translation unit.In practice using
extern
means in your header file you want to write:But in one and only one other place that isn't a header:
static
全局变量是每个 .c 或 .cpp 文件(或翻译单元)的私有变量。如果打印出read_mess
的地址(例如printf("%x", &read_mess);
),您将看到不同的地址,这意味着两个单独的副本布尔变量的存在。解决方案是删除
static
关键字,或替换为extern
。并且,在任何 .c 或 .cpp 文件中仅将该变量的定义放置一次。static
global variables are privates to each .c or .cpp file (or translation unit). If you print out the address ofread_mess
(e.g.,printf("%x", &read_mess);
), you will see different addresses, which means two separate copies of the boolean variable exist.A solution would be remove
static
keyword, or replace withextern
. And, place the definition of that variable only once in any .c or .cpp file.当您在头文件中声明静态变量时,会在每个 包含该文件的翻译单元(标头+源文件)。
您正在检查静态变量的值,该变量是为该翻译单元定义的副本,它与您在另一个翻译单元中初始化的变量不同。
如果您想跨不同文件访问变量,最好使用
extern
。utility.h - 将其包含在您想要访问
read_mess
的所有文件中File1.cpp - 定义
read_mess
源文件之一File2.cpp - 访问任何源文件中的
read_mess
此外,
C++03 标准:7.3.1.1/2 表示:< /强>
When you declare a static variable in a header file an copy of the staic variable gets created in each Translation unit(headers+source file) in which the file is included.
You are checking the value of the static variable which is a copy defined for that translation unit, it is not the same as the one you initialized in another translation unit.
You are better off using a
extern
if you want to access the variable across different files.utility.h - Include this in all files where you want to access
read_mess
File1.cpp - Define
read_mess
in one of the source filesFile2.cpp - Access
read_mess
in any of the source filesAlso,
C++03 standard: 7.3.1.1/2 says: