-
static variables
If global variables are frowned upon, then I can say the same about static variables. They are usually all the time made public.
Code:
class myClass
{
public:
myClass() {}
static int s_x;
};
int myClass::s_x = 10;
int main ( void )
{
std::cout << "Value of static x: " << myClass::s_x << std::endl;
return 0;
}
[
Am I wring in the assumption?
Their value is held between function calls, so is a global variables, and is not lost when the function returns. So why would you use static when global do the same thing?
-
Functionally, a static data member of a class behaves identically to a global variable of the same type. This means the tradeoffs (up sides vs down sides) of the two are identical.
In practice, I have rarely seen cases where static members of a class are actually public. In fact, in my experience, it is much more usual for them to be private (and manipulated by member functions), somewhat less common for them to be protected (and manipulated bu member function of the class or a derived class), and most rare for them to be public (unless the programmer is lazy, and misusing a static member just like another lazy programmer might misuse a global variable). And this reflects the advantage of static data members of a class over global variables: it is much easier to prevent unwanted access or modification of static data in a class.
-
You have to understand thereasons why global variables are frowned upon in order to better understand why static variables don't compare.... and in fact sometimes they can be used as a replacement(!) of global variables.
Reasons for not using global variables:
1. Makes it hard to reuse code. Any code that use global variables is dependant on those gobal variables. You can not reuse it on other projects (or other parts of your project) without having to move your global variables too.
2. Makes it hard to maintain code. You stand, looking at that global variable you wrote a few months ago. What parts of the code use it? You surely remember some, but almost certainly not all. You need to change its value because it was found that the solar system has 7 planets now, not 8. Something you took for granted, was not after all. But what impact will it have on existing code? Where is the code that uses it? These questions become even more hard to answer if you were not the one developing the code in the first place.
Remember always the reason why you wrote a global variable in the first place. Almost always it was because it was more convenient at that time. It was faster to just put the problem aside in the form of a global variable. But... it's code maintenance that takes more time. Not developing. So, you just added a layer of complexity to code maintenance, increasing its time cost considerably.
And sometimes it is exactly a static variable that will be the best replacement. Consider a class that handles your solar system. Imagine a static variable that stores the number of planets on the solar system. The variable is contained within the class. It can even be private! And any code using that variable is also contained within that class, making it much easier to answer which code uses that variable and what will happen if you change its value.