Static member of a class

1. Static Variable of Class Member

 

Static member variables are just like static variable that we saw in the previous blog. The only way it differs is it can be accessed through the scope resolution operator [::] using the class name in which it is declared.

 

The lifetime is same as the normal static variable. One more important point! There will be only one copy of the static member and it does not matter how many instance you create.

 

class StaticMemberTest

{

public:

            StaticMemberTest();

            ~StaticMemberTest();

            static int TotalInstance;

};

 

In the above code, the static member is kept in the class StaticMemberTest. You can access the static member using the class and scope resolution operator as shown below:

StaticMemberTest::TotalInstance

 

Or you can access it using the object of the class. All the objects of the class StaticMemberTest will share the single copy of the static member.

 

2. Initializing the member

 

As we know the static member comes alive when the program starts, it needs to be initialized even before the object of the containing class gets created. To do that after the class definition, the static member should be initialized. Consider the above piece of code; the member can be initialized as shown below:

 

//Initializing the static member of a class

int StaticMemberTest::TotalInstance = 0;

 

3. The example

 

1) First a class is created with a static member in it.

 

// CPPTST.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <conio.h>

 

//Class Having static Member

class StaticMemberTest

{

public:

            StaticMemberTest();

            ~StaticMemberTest();

            static int TotalInstance;

};

 

 

2) After the class definition the static member is initialized. When you do initialization you should specify the type also.

 

//Initializing the static member of a class

int StaticMemberTest::TotalInstance = 0;

 

3) In the constructor the member is initialized using the scope resolution operator ::). In the destructor also it is accessed in the same way. Note that the scope resolution operator usage outside the class in the previous step. 

 

//Constructor

StaticMemberTest::StaticMemberTest()

{

            TotalInstance++;

            printf("Message from Constructor: Total Object Created. = %d\n",  StaticMemberTest::TotalInstance );

}

 

//Destructor

StaticMemberTest::~StaticMemberTest()

{

            TotalInstance--;

            printf("Message from Destructor:  Total Object Created = %d\n",  StaticMemberTest::TotalInstance );

}

 

4) In the main program two sets of curly braces are placed to destroy the objects. The internal brackets create Obj1, Obj2, Obj3 and Obj4 and destroy it immediately. The objects Obj and obj5 are placed in the outer curly braces.

 

int _tmain(int argc, _TCHAR* argv[])

{

            {

                        //Create the Static Instance

                        StaticMemberTest obj;

 

                        //Create some more instance and destroy it immediately

                        {

                                    StaticMemberTest obj1, obj2, obj3, obj4;

                        }

 

                        //You can access the static member like other member also.

                        StaticMemberTest obj5;

                        printf("Total Object Created = %d\n", obj.TotalInstance );

                        printf("Total Object Created = %d\n", obj5.TotalInstance );

            }

            getch();

            return 0;

}

 

The code in the main proves you that all the objects share the same static variable TotalInstance

 

 

Below is output of the program:

 

Pic01.JPG