不理解静态布尔行为

发布于 2024-12-05 22:03:38 字数 794 浏览 0 评论 0原文

我有一个头文件,其中有一些静态变量供我的所有文件使用。我在那里有一个初始化为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2024-12-12 22:03:38

在此上下文中,static 表示“本地”(对于翻译单元)。您的程序中将有多个 read_mess 副本,每个翻译单元一个,这与头文件不同。 (在您的情况下,您很可能将“翻译单元”近似为 .cpp 或 .c 或 .cc 文件)。

可能您想要做的是声明一个extern变量,或静态类成员定义 它仅在一个翻译单元中。

在实践中,使用 extern 意味着您要在头文件中编写:

extern bool read_mess;

但在一个且仅有一个其他不是标头的地方:

bool read_mess = false;

static in this context means "local" (to the translation unit). There will be multiple copies of read_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, or static class member and define it in just one translation unit.

In practice using extern means in your header file you want to write:

extern bool read_mess;

But in one and only one other place that isn't a header:

bool read_mess = false;
深海里的那抹蓝 2024-12-12 22:03:38

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 of read_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 with extern. And, place the definition of that variable only once in any .c or .cpp file.

不可一世的女人 2024-12-12 22:03:38

当您在头文件中声明静态变量时,会在每个 包含该文件的翻译单元(标头+源文件)。

您正在检查静态变量的值,该变量是为该翻译单元定义的副本,它与您在另一个翻译单元中初始化的变量不同。

如果您想跨不同文件访问变量,最好使用 extern

utility.h - 将其包含在您想要访问 read_mess 的所有文件中

extern bool read_mess;  

File1.cpp - 定义 read_mess源文件之一

#include"utility.h"

bool read_mess = false;

File2.cpp - 访问任何源文件中的read_mess

#include "utility.h"

if(read_mess)
{
    //do what interests you
}

此外,

C++03 标准:7.3.1.1/2 表示:< /强>

在命名空间范围内声明对象时,不建议使用 static 关键字,unnamed-namespace 提供了一个更好的替代方案。

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

extern bool read_mess;  

File1.cpp - Define read_mess in one of the source files

#include"utility.h"

bool read_mess = false;

File2.cpp - Access read_mess in any of the source files

#include "utility.h"

if(read_mess)
{
    //do what interests you
}

Also,

C++03 standard: 7.3.1.1/2 says:

The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文