Static Variables in C ++

I would like to know what is the difference between static variables in vs header file declared in class. When a static variable is declared in the header file, its scope is limited to the .h file or all elements. Also, as a rule, a static variable is initialized in a .cpp file when it is declared in a class correctly? So this means that the scope of the static variable is limited to two compilation units?

+42
c ++ scope static
Sep 13 2018-10-10T00:
source share
3 answers

Excuse me when I answer your questions out of order, this makes it easier to understand.

When a static variable is declared in the header file, its scope is limited to the .h file or all units.

There is no such thing as a "file header area". The header file is included in the source files. A translation block is a source file that includes text from header files. Everything that you write in the header file is copied to each of them, including the source file.

Thus, the static variable declared in the header file is similar to the static variable in each individual source file.

Since declaring a static variable in this way means internal binding, each unit of translation #include in your header file gets its own variable , individual (which is not visible outside your translation unit). This is usually not what you want.

I would like to know what is the difference between static variables in vs header file declared in class.

In a class declaration, static means that all instances of the class share this member variable; those. you can have hundreds of objects of this type, but whenever one of these objects refers to a static variable (or "class"), this is the same value for all objects. You might think of it as a "global class."

Typically, a static variable is initialized in a .cpp file if it is declared in the class correctly?

Yes, one (and only one) translation unit must initialize a class variable.

So, what does it mean that the scope of a static variable is limited to two compilation units?

As I already said:

  • The header is not a compiler,
  • static means completely different things depending on the context.

Global static limits the scope of translation. The static class means global for all instances.

Hope this helps.

PS: Check out the last paragraph of Chubsdad's answer on how you shouldn't use static in C ++ to specify internal binding, but anonymous namespaces. (Because he is right. ;-))

+64
Sep 13 '10 at 6:19 06:19
source share

Static variable in the header file:

say 'common.h' has

 static int zzz; 

This variable 'zzz' has an internal relationship (the same variable cannot be obtained in other translation units). Each translation unit that includes 'common.h' has its own unique object called 'zzz' .

Static variable in class:

A static variable in a class is not part of a subobject of the class. There is only one copy of the static data element, shared by all objects in the class.

$ 9.4.2 / 6 - "The static data elements of a class in the namespace have external (3.5). The local class should not have static data elements."

So let 'myclass.h' have

 struct myclass{ static int zzz; // this is only a declaration }; 

and myclass.cpp has

 #include "myclass.h" int myclass::zzz = 0 // this is a definition, // should be done once and only once 

and "hisclass.cpp" has

 #include "myclass.h" void f(){myclass::zzz = 2;} // myclass::zzz is always the same in any // translation unit 

and "ourclass.cpp" has

 #include "myclass.h" void g(){myclass::zzz = 2;} // myclass::zzz is always the same in any // translation unit 

Thus, static class members are not limited to just two translation units. They must be defined only once in any of the translation units.

Note: using 'static' to declare a file region variable is deprecated and the unnamed namespace is excellent.

+38
Sep 13 '10 at 6:09
source share

The static variable declared in the header file outside the class will be file-scoped in every .c file that includes the header. This means that a separate copy of the variable of the same name is available in each of the .c files where you include the header file.

On the other hand, a class-scoped static variable and the same static variable is available for every compilation unit, which includes a header containing a class with a static variable.

+12
Sep 13 '10 at 6:14
source share



All Articles