Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexposed private member variables vs. global variables in the source file

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

Is there an advantage to this over declaring the variable private? Is this bad practice? Or is this normal when declaring static variables that are used by the class only and nobody else?

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

like image 969
Samaursa Avatar asked Mar 28 '11 18:03

Samaursa


2 Answers

From a stylistic point of view, this might, or might not, be okay, but style is subjective.

From a technical point of view, there are a couple of 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, a static variable at file scope (or a variable in an anonymous namespace) can be more private, except that it is visible to all code that follows it in the source file (which changes the accessibility somewhat).

I personally prefer them for those objective reasons. I tend to keep my headers as empty as possible, because it makes changes with no impact on the client much easier (and I am the client most of the times!)

Note: if I have forgotten differences, please do tell/edit

like image 157
Matthieu M. Avatar answered Nov 15 '22 15:11

Matthieu M.


The main advantage to this way is reducing the amount of "unnecessary" stuff in the *.h file. This might slightly improve compile times and/or rebuild complexity when files or modified, and it may make the header file slightly easier to read.

(In my opinion, these advantages are small, and I will usually prefer the clarity of putting things that logically are related to a class in that class's scope.)

However, static global variables are deprecated and bad practice in C++. If there is no other appropriate scope, an anonymous namespace should be used.

// Instead of this:
static std::list<MyClass*> MyClass_population;

// Do this:
namespace { // anonymous
    std::list<MyClass*> MyClass_population;
}
like image 32
aschepler Avatar answered Nov 15 '22 14:11

aschepler