Don't understand static boolean behavior
I have a header file that has some static variables for all of my files to use. I have a boolean variable开发者_StackOverflow中文版 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?
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;
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.
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.
精彩评论