I would like to know what is the difference between static variables in a header file vs declared in a class. When static variable is declared in a header file is its scope limited to .h file or across all units. Also generally static variable is initialized in .cpp file when declared in a class right? So that does mean static variable scope is limited to 2 compilation units?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Excuse me when I answer your questions out-of-order, it makes it easier to understand this way.
There is no such thing as a "header file scope". The header file gets included into source files. The translation unit is the source file including the text from the header files. Whatever you write in a header file gets copied into each including source file.
As such, a static variable declared in a header file is like a static variable in each individual source file.
Since declaring a variable
static
this way means internal linkage, every translation unit#include
ing your header file gets its own, individual variable (which is not visible outside your translation unit). This is usually not what you want.In a class declaration,
static
means that all instances of the class share this member variable; i.e., you might have hundreds of objects of this type, but whenever one of these objects refers to thestatic
(or "class") variable, it's the same value for all objects. You could think of it as a "class global".Yes, one (and only one) translation unit must initialize the class variable.
As I said:
static
means completely different things depending on context.Global
static
limits scope to the translation unit. Classstatic
means global to all instances.I hope this helps.
PS: Check the last paragraph of Chubsdad's answer, about how you shouldn't use
static
in C++ for indicating internal linkage, but anonymous namespaces. (Because he's right. ;-) )Static variable in a header file:
say
'common.h'
hasThis variable
'zzz'
has internal linkage (This same variable can not be accessed in other translation units). Each translation unit which includes'common.h'
has it's own unique object of name'zzz'
.Static variable in a class:
Static variable in a class is not a part of the subobject of the class. There is only one copy of a static data member shared by all the objects of the class.
So let's say
'myclass.h'
hasand
myclass.cpp
hasand
"hisclass.cpp"
hasand
"ourclass.cpp"
hasSo, class static members are not limited to only 2 translation units. They need to be defined only once in any one of the translation units.
A static variable declared in a header file outside of the class would be
file-scoped
in every .c file which includes the header. That means separate copy of a variable with same name is accessible in each of the .c files where you include the header file.A static class variable on the other hand is
class-scoped
and the same static variable is available to every compilation unit that includes the header containing the class with static variable.