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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include "stdafx.h" #include "conio.h" class CRectangle { public: //Sample 01: Constructor CRectangle(int L, int W) { m_lenght = L; m_width = W; count++; } //Sample 02: Static Method static int GetCount() { return count; } private: //Sample 03: Static count declaration with other declarations static int count; int m_lenght; int m_width; }; //Sample 04: Initializing static member int CRectangle::count = 0; int _tmain(int argc, _TCHAR* argv[]) { //Sample 05: Create two Objects CRectangle R1(3,3); CRectangle* R2 = new CRectangle(4,4); //Sample 06: Print the count using the object name printf("Total Objects Created %d\n", R2->GetCount()); //Sample 07: Print the count using Class Name CRectangle R3(1,3); printf("Total Objects Created %d", CRectangle::GetCount()); //Sample 08: If you try accessing the count directly, //you get error as the static member is still //respects the scope private. // CRectangle::count => Even though the count // can be accessed by using ::, // the private scope prohibits that // printf("Total Objects Created %d\n", CRectangle::count ); getch(); } |
3. Code Explanation
3.1 Declare variables
We have declared three private member variables in the class
CRectangle
. In these declarations, we also declared a static variable called
count
.
1 2 3 4 5 |
private: //Sample 03: Static count declaration with other declarations static int count; int m_lenght; int m_width; |
3.2 The Constructor
In the constructor, we initialize the normal Member Variables to a standard value. But we increment the Static Variable called
count
. Note, whenever we create the object, the static
count
gets increased by 1.
1 2 3 4 5 6 7 |
//Sample 01: Constructor CRectangle(int L, int W) { m_lenght = L; m_width = W; count++; } |
3.3 GetCount Static Member Function
Then we placed a static method
GetCount
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
CRectangle::GetCount
. 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.
1 2 3 4 5 |
//Sample 02: Static Method static int GetCount() { return count; } |
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:
1 2 |
//Sample 04: Initializing static member int CRectangle::count = 0; |
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
R1
created in a stack and other one is for the object created in a heap pointed by
R2
. The below picture shows this.

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
R1
and Object pointed by
R2
. 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:
1 2 3 |
//Sample 05: Create two Objects CRectangle R1(3,3); CRectangle* R2 = new CRectangle(4,4); |
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
R1
object you can access the static method like
R1.GetCount()
.
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
GetCount
declared as static in the class helps us to call the method with the class name and scope resolution operator.
1 2 3 |
//Sample 07: Print the count using Class Name CRectangle R3(1,3); printf("Total Objects Created %d", CRectangle::GetCount()); |
Accessing the count through class name like
CRectangle::count
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.
1 2 3 4 5 6 7 |
//Sample 08: If you try accessing the count directly, //you get error as the static member is still //respects the scope private. // CRectangle::count => Even though the count // can be accessed by using ::, // the private scope prohibits that // printf("Total Objects Created %d\n", CRectangle::count ); |
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
Categories: C++
Tags: Static Member Scope, Static Member Visibility, Static Members