Sunday, Nov 18th

Last update12:59:40 PM GMT

Static Member Variables

Write e-mail

This article assumes you know well about how static variables and static functions work in C. C++ retains the same understanding about them and further extends static keyword to classes. In C++ classes, you can have static member data and static member functions as well.

argaiv1879

Static member variables
See the below example of a class declaration:


class myClass
{
private:
int my_var;
public:
myClass(){my_var = 0};
int my_func()) {
my_var ++;
return my_var;
}
};
int main()
{
myClass First;
myClass Second;
int a = First.my_func();
int b = Second.my_func();
return 0;
}

In the above program, when we instantiate a class object, each object gets it’s own copy of all normal member variables. In this case, because we have declared two instances of myClass, we end up with two copies of my_var — one inside First, and other inside Second, both entirely independent from each other. At creation, the constructor of both the objects initialises, their respective copies of my_var to 0. Subsequently when we call my_func on both the objects, the respective copies get incremented. Hence both variables a and b contain the value 1.

Member variables of a class can be made static by using the static keyword. Only one copy of static member variables exists in a program regardless of how many class objects are defined! We can say that, all objects of a class share the static variables. Consider the following program:

class myClass
{
private:
static int my_var;
public:
int my_func()) {
my_var ++;
return my_var;
}
};
int myClass::my_var = 1;
int main()
{
myClass First;
int a = First.my_func();
myClass Second;
int b = Second.my_func();
cout<”a: “<<<” and b: “<

return 0;
}
The output of above program is:
a:1 and b:2

Explanation: Since my_var is a static member variable, its value is shared between all objects of myClass. That is to say, both objects First and Second operate on exactly the same copy of my_var. Hence multiple calls of my_func (even if done through different objects) increment the same copy of my_var.

Now suppose, my_var was public in the class myClass. So obviously it can be accessed as First.my_var. But this usage is rather misleading. Because static variables do not actually belong to a particlular object rather they belong to a class, hence it is better to access it directly using the class name and the scope operator like: myClass::my_var;

In fact, my_var exists even if no objects have been instantiated in the program!!

For the same reason to initialise a static member variable to a non-zero value, you must explicitly define the static member. The following line in the above snippet does this:

int myClass::my_var = 1;

This statement should be placed in the code file (cpp) for the class. In the absence of an initializing line, C++ will initialize the static variable to 0.
Some logical scenarios where static variable members come handy are:

  • To implement a lookup table
  • To keep a count of how many objects are presently instantiated in the program at any time.

These are two I could recall. Of course you will be able to think of many more :)

Tags:

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting