top of page

Static Members

Those of you who are familiar with C language, have used static keyword which is opposite of automatic. A static variable has a lifetime of complete program execution. That is to say it does not get destroyed when you leave the block where it is defined. That is to say that a static variable stays there until the program ends.


What does static keyword mean in C++?

Static storage


Automatic variables are destroyed when a function or block in which they are defined exits  where as static variables have static duration. That is they remain alive till the end of program. 

And static global variables and functions have internal linkage. That is- they are visible only in the file(translation unit) they are defined in.  A non-static global variable or function has external linkage. It is visible in the entire program.

 

Let us look at a simple program.

What will be the output of the program?

g=2  s=2
g=2  s=3
g=2  s=4

 

So what happens here? Since g is an automatic variable, it is created and destroyed each time print() function is called. So g++ makes g as 2 each time. But s is static. So it is created and initialized only first time. Subsequent calls to print() function just increment s. So s is 2 , 3 and 4.

 

Now let us discuss about static members of a class.

Static member of a class does not belong to an object, but it belongs to the class.

Static data member

 

Ordinary data members are stored in each object and they are different for each object. But a  static data member is shared by all objects of the class. There is only one copy of static data member. (If you want, you can observe this by looking at the size of an object having static data.)

 

To make a data member static, it should be defined with keyword static inside the class body.

 

A static data MUST also be defined again outside the the class with scope resolution operator, because inside class, the declaration of static data is not definition.

In the code above, rate is a static data member. And it is defined outside SimpleInterest class using scope resolution operator (::).

 

Static data members can be accessed using dot or arrow operators like any other members of the class. But they can also be accessed without an object, using class name followed by scope resolution operator.

 

In the above example, we can access rate as

SimpleInterest::rate

Output:

0.085 0.085

​​

In the example above, we are accessing rate using :: operator and dot operator both. In first line of main() we accessed rate without creating any object.

 

So a static members of a class can be accessed without creating any object of the class.

we are assigning a value to rate using ::. Then we are setting the value of rate using obj. When obj2.rate is printed, we see the same value 0.085.

This shows that, unlike other data members, static members are shared by all objects of a class. In this case, rate is one integer which is shared by all objects of SimpleInterest class. 

 

Try it out : Find out the size of a class with a static data member in it using sizeof() operator Does the size include this static member?

 

Also remember that a static data member can not be mutable.

Static member functions:

 

A static member function is a method which can be invoked without using any object. Static function can be called directly using  class name and scope resolution operator.

But there is a catch. A static function can access only static data members. It can not access non-static data because it does not belong to an object. It has no "this" pointer.

 

A function is made static by using static keyword along with function declaration within the class body.

 

Rules for static functions.

  1. A static function can not use "this" pointer. Hence it can not access non-static data members.

  2. A static function can not be constant.

  3. Static function can not be virtual.

 In the code shown above, we are not creating any object of A . We can still call the print() method of class A using class name. 

If in static function print(), we try to access n (a non-static member), we get a compilation error.  
 

Let us look at an example where a static function will help us to create objects even if we have only private constructor to a class.

In this example class A has only a private constructor. So statement of the type A obj; outside of class will not compile because the call can not access the constructor.

 

Instead we are having a static function getInstance() which creates a new object dynamically and returns its address. 

The code returns an existing pointer if there is already an object of the class. So we can only create one object of this class - it is a singleton class. 

bottom of page