Unpublished private member variables compared to global variables in the source file

I look at the code in the company I currently work for and see several (not many) declarations of static global variables in *.cpp files (for example, to store a list of listeners), where the .h/.cpp files belong to the class. If a variable (static or otherwise) that is used only by the class itself, I always declare it private.

Are there any advantages to this when declaring a private variable? Is this a bad practice? Or is this normal when declaring static variables that are used only by the class, and no one else?

EDIT: In my question, I asked about statics, but what if it's a non-static global variable in a .cpp file instead of being a private member of a class? Is this bad practice or is it considered normal? Any benefits in this case?

+4
source share
6 answers

The main advantage of this method is the reduction in the number of "extra" things in the * .h file. This can slightly improve compilation time and / or rebuild complexity when files or changes, and can make the header file a little easier to read.

(In my opinion, these advantages are small, and I usually prefer the clarity of placing things that are logically related to the class in this area of ​​the class.)

However, static global variables are obsolete and bad practice in C ++. If no other suitable scope exists, use an anonymous namespace.

 // Instead of this: static std::list<MyClass*> MyClass_population; // Do this: namespace { // anonymous std::list<MyClass*> MyClass_population; } 
+4
source

From a stylistic point of view, this may or may not be in order, but the style is subjective.

From a technical point of view, there are several differences:

  +----------------+-------------+ | Private Static | File Static | +----------------------------+----------------+-------------+ | Visible by includers | Yes | No | +----------------------------+----------------+-------------+ | Accessible to friend | Yes | No | +----------------------------+----------------+-------------+ | Accessible to all in TU* | No | Yes | +----------------------------+----------------+-------------+ | Require #include in header | Yes | No | +----------------------------+----------------+-------------+ *TU: Translation Unit (roughly put: the source file after include resolution) 

Technically, therefore, the static variable in the file area (or the variable in the anonymous namespace) can be more confidential, except that it is visible to all the code that follows it in the source file (which changes accessibility somewhat).

I personally prefer them for those objective reasons. I try to keep my headers as empty as possible, because it makes changes without any effect on the client much easier (and I am most often the client!)

Note: if I forgot the difference, say / edit

+3
source

Static is only meant if you want the variables to be useful in this file, and for private only in this class. In some cases, they do roughly the same thing, but I prefer to statically set it first, because it is a compile-time variable compared to an automatic run-time variable. Although this depends on compiler optimization.

0
source

I usually do this when there is a function or variable that I want to share between classes that are in the same file. In Java, I think it can be (I guess here) achieved through inner classes. I find this harmless if the external user-visible header file interface is independent of them.

0
source

There is no such thing as a global static variable: what you are looking for are static variables with a file scope. (At least my C ++ teacher did not name these global ones, I'm not sure what the standard calls them.)

However, they have all the advantages of global variables and all the disadvantages of global combinations, except for one: they do not collide with each other if they have the same name.

For example, the following will not be referenced:

 // in a.cpp static int a; int b; // in a.cpp static int a; int b; 

because the global symbol b occurs twice. a , however, is not a problem.

The difference with a static class variable is that they appear in the header, so changing, deleting, or adding them requires recompiling all of the client code.

0
source

Like you, I usually prefer to use class static in the .h file instead of the global static in the .cpp file.

However, there is one case where I prefer global static . This happens when it avoids the need for the #include directive (to access the declaration of the static variable type) in the .h file.

0
source

Source: https://habr.com/ru/post/1345593/


All Articles