2

What are the major differences between the static class member variable and the static variable ? Both static class members and static varibles are accessed from member functions of any class. What are the specific uses of static class members and static varibles?

3
  • The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables. Commented May 5, 2015 at 7:26
  • static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time Commented May 5, 2015 at 7:27
  • Only one copy of static class member is created and shared by all objects Commented May 5, 2015 at 7:40

5 Answers 5

5

The only reason is code cleanliness. You cannot restrict access to a global static variable like

static int globalValue=5;

it is (at least) visible in the source file you defined it. With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:

class myGlobalContainer
{
   public:
   static int myInt;
   protected:
   static float myFloat;
   private:
   static bool myBool;
};

the access of myInt is done by:

int x=myGlobalContainer::myInt;

the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.

The modifier protected and private shows that you do not wish that an "outsider" access those values.

protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:

class myGlobalContainer
{
   public:
     myGlobalContainer() 
     {
       if(counter==0)
         DoSomeSpecialGlobalInit();
       counter++;
     }
     ~myGlobalContainer() 
     {
       counter--;
       if(counter==0)
         DoSomeSpecialGlobalUnInit();
     }
   private:
   static int counter=0;
};

public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:

COLOR white=COLOR::WHITE;

instead of:

COLOR white=COLOR::FromAGBR(255,255,255,255);

Add least: If you should use statics or not is a complete other discussion.

1

Both static class members and static varibles are accessed from member functions of any class.

That is not true:

class A {
private:
    static int x;
};
int A::x = 5;

class B {
    static int y;
public:
    void do_something()
    {
        std::cout << A::x; // Can't access A::x because it's private
    }
};
int B::y = 10;

Although, if we did this:

static int J = 9;

class A {
private:
    static int x;
};
int A::x = 5;

class B {
    static int y;
public:
    void do_something()
    {
        std::cout << J; // Yes, J is global.
    }
};
int B::y = 10;
  • Static member variables can access the private section of it's class opposed to a normal static variable.
  • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.
  • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.

Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.

1
  • Correct. But public static class members can be accessed by member functions of any other class. Commented May 5, 2015 at 10:02
0

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.

If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..

A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

3
  • How is it different than ordinary non-class static variable? Commented May 5, 2015 at 7:29
  • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object
    – anshabhi
    Commented May 5, 2015 at 8:03
  • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..
    – anshabhi
    Commented May 5, 2015 at 8:09
0

My list of differences:

  1. You can make static class member protected or private.
  2. You can't make static class member global.

Can't think of anything else.

0

Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.

Static variables inside a function are variables that retain its value through function calls. A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.

Not the answer you're looking for? Browse other questions tagged or ask your own question.