Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Static Members in C++ And Its Role

1. What is Static Members?

The Static Members of the class is good to store information which is shared between the class instances. Consequently, this member does not belong to individual objects created from the class template. Simply, all instances of a class share the same static data member declared in the class. When we talk about static data member, we should be familiar with the scope and its visibility.

Scope tells where one can access the variables and visibility talks about the lifetime. We will explore more about this while we progress.

2. Code Example

The below code snippet shows an example for Static Data Members. We explore more about this in the following texts.

3. Code Explanation

3.1 Declare variables

We have declared three private member variables in the class


. In these declarations, we also declared a static variable called



3.2 The Constructor

In the constructor, we initialize the normal Member Variables to a standard value. But we increment the Static Variable called


. Note, whenever we create the object, the static


  gets increased by 1.

3.3 GetCount Static Member Function

Then we placed a static method


to return the count. Note, this method is in public scope and hence it can be accessed from outside the class. When we have the method as static, we can access only the static members in it. You know why? Because, we can access the static method without creating the instance of the class object like


 . As the static function scope is public, it can be accessed anywhere say for example inside the member function or outside of class.

When a static member is private, one can accessed it without creating the object. But the restriction is that only by the member function of a class can access it.

3.4 Initialize Static Data Member

The Static Data Members does not belong to a particular object. It is common for all the class instances. C++ initializes these variables when program load into memory. The below code shows how the Static Member is initialized to zero when program loads:

3.5 Static Data Member Shared By Multiple Objects

In the main function, we are creating two rectangle objects. So, C++ calls the constructor twice. One is for the object


created in a stack and other one is for the object created in a heap pointed by


. The below picture shows this.

Static Data Member Shared by Objects

Static Data Member Shared by Objects

There are two copies of m_length and m_width. However, there is only one copy of the member count for both


and Object pointed by


. Now remember, when we created two Objects, C++ calls the constructor for both the Objects. As there is only one copy of count that can be accessed by both the Objects, the count increments to two from zero. The code is below:

The console output statement in the below code prints two. Also note, we are accessing the static method through a pointer to member


. If we use the


object you can access the static method like



3.6 Static Member and Scope Resolution Operator ‘::’

In the below code, we called the static method through its class name. The stuff we should note here is that the


  declared as static in the class helps us to call the method with the class name and scope resolution operator.

Accessing the count through class name like


  is not possible in our example. Because the static count is under the private scope. In order to compile the code, One can remove the comment in the below code and move the static count variable to a public section. So, even though the static can be accessed through the class name without creating the object, the variable still respects the scope which is private.

Note, once can declare the static variable in side the global function as well. Here, the visibility is inside the function and scope is throughout the program run time. The output of running the example with commented section, Sample 08 is below:

C++ Static Data Member Example Output

C++ Static Data Member Example Output

Categories: C++

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.