Thread: static variables

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    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?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.
    Last edited by Mario F.; 08-28-2006 at 06:42 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  2. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  3. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 09:21 AM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 03:45 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM