Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Okay to declare static global variable in .h file?

static keyword keeps the scope of a global variable limited to that translation unit. If I use static int x in a .h file and include that .h file every other file, won't they all belong to the same translation unit? Then, won't x be visible everywhere? So what is the role of static now?

Also, is there any use of static const int x ,where x is a global variable? Aren't all const global variables static by default? And is a const variable's scope limited to the TU even if it confined in a for loop in the file?

like image 242
batman Avatar asked Aug 15 '12 10:08

batman


People also ask

Can we declare static variable in .h file?

Yes there is difference between declaring a static variable as global and local. If it is local, it can be accessed only in the function where it's declared. But if it is global, all functions can access it.

Can we declare global variable in header file?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

Should I use static global variables?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.

What happens when a global variable is declared as static?

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. In C, the preprocessor directive #define was used to create a variable with a constant value. This still works in C++, but problems could arise.


2 Answers

If you write

static const int x

in an .h file then every translation unit that #include-s this .h will have its own private variable x.

If you want to have 1 global variable visible to everyone you should write

extern const int x;

in the .h file and

const int x = ...;

in one of the .cpp files.

If you want to have a static const int visible to just one translation unit - don't mention it in the .h files at all.

like image 165
Alexander Chertov Avatar answered Oct 12 '22 12:10

Alexander Chertov


If I use static int x in a .h file and include that .h file every other file, won't they all belong to the same translation unit?

If you declare something as static (not inside a class, for class static keyword has a different semantic), that static variable cannot be seen outside its TU. So putting it in the header file will cause each TU including that header to have a different private copy of that static variable.

And is a const variable's scope limited to the TU even if it confined in a for loop in the file?

NO. Even for a static const value, the scope is determined by it's declaration. So the scope will be limited by your for brackets.

like image 33
Heisenbug Avatar answered Oct 12 '22 12:10

Heisenbug