开发者

Does each file get its own copy of the static variable?

Suppose you have a static global variable in your header file, and you use this variable in your main.cpp.

// header.h
static int variableOne = 100;

//main.cpp
   .
   .
   cout << variableOne << endl;

Will main.cpp get its own copy of variableOne (although the value is still 100...)? Or am I mixing this concept with extern (I know that extern 开发者_StackOverflow中文版tells the compiler that variableOne is defined elsewhere in the project...)

Thank you.


If you declare a static variable in an header file then a copy of that variable will be created in each translation unit where you include the header file.

So Never declare a static variable in Header File.

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.

C++03 standard: 7.3.1.1/1 says:

"[a]lthough entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit."

In simple words, an unnamed namespace restricts the visibility of its members to the scope of the translation unit by means of name mangling and avoids the problems faced as while using static keyword.

Also, You cannot use keywords static and extern together on a variable because both aim to achieve mutually exclusive behaviors.


In this case, each compilation module will get it's own copy of the variable.

If you use extern, there will only be one copy. But you are only allowed to initialize it in one module.

In other words, if you just replace static with extern in your example, it won't compile because it's being initialized in every module that includes that header.


1. What does it mean #include

When you write #include "header.h" the compiler is basically just copying the whole content of the file instead of that line. In C++ an header file has no semantic meaning, it's just used for textual substitution.

So if you have

---- header.h ----
static int variableOne = 100;

---- main.cpp ----
#include "header.h"

...
std::cout << variableOne << std::endl;

for the compiler is exactly the same as

---- main.cpp ----
static int variableOne = 100;

...
std::cout << variableOne << std::endl;

2. What does it mean static in that context

A static variable is like a global for the lifetime (i.e. it's constructed before main starts and it's destroyed after main terminates) but is only visible inside the translation unit that defines it. So you can have the same static variable name used in different translation units and all those variables are distinct (each translation unit will see its own static variable).

3. What happens when you put a static variable declaration in an header?

The net result is that every translation unit that will include that header will get its own static variable with that name because it's exactly the same as if the translation unit defined a static variable in the .cpp and not in the header file.

The name will be the same for all of them but they will be different variables. Of course if the header file declares and initializes the variable then the initial value will be the same, but all those variables will be distinct and if for example one translation unit changes that variable, the change will not be seen by other translation units.


Each source file would get their own independent copy of variableOne.

The last use of static is as a global variable inside a file of code. In this case, the use of static indicates that source code in other files that are part of the project cannot access the variable. Only code inside the single file can see the variable.

Source

If you use extern all your source files will reference the same variable, but you won't be able to initialize it in the header. You would have to add an initialization to one of the source files.


static variables in namespace scope are not external, and as such you would get a copy of the static variable for each 'translation unit' in the project. This technique has been obsoleted since C++03, and the recommended way to do it is now enclosing the variable within an unnamed namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜