Static Members & Member Functions in C++

In this tutorials we will study and understand the concept and functionality of Static Members & Member Functions in C++. If you don’t know what classes and objects in c++ mean check this tutorial

Static Data Members


static data members in c++

In normal situations when we instantiate objects of a class each object gets its own copy of all normal member variables. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. This means one single copy of that data member is shared between all objects of that class. All static data is initialized to zero when the first object is created, if no other initialization is present. Static data members can be initialized outside the class using the scope resolution operator (::) to identify which class it belongs to. Since Static data members are class level variables, we do not require the object to access these variables and they can be accessed simply by using the class name and scope resolution(::) operator with the variable name to access its value. Also a static data member cannot be private. A very common use of static data members is to keep a count of total objects of a particular class (program counter kind of application)

Run Online

Total Objects: 1
Total Objects: 2
Program Explanation

In this program, we have created a class Cube with 1 normal variable and 1 static variable. In the default constructor of this class we are incrementing the static variable objectCount value by 1. So everytime an object is created this value will be incremented. In the main() function we create 1 object and print the objectCount variable using the classname and (::) scope resolution operated. Since we created 1 object, its default constructor was called which incremented the value of objectCount variable by one and hence we get the output 1. Then we again create one more new object c2 and again print the objectCount value. This time however it prints 2 since the default constructor for object c2 was called again and it incremented the objectCount value by 1 again and since this is a static member it is shared between both the objects c1 and c2 so they will have same values that is 2.

Static Member Functions

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator (::)
A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to the this pointer of the class.

Run Online

Program Explanation

If you compare the first program and this program, the only difference is that in this program we have created a static member function named getCount() which returns the static data member objectCount value. Since getCount is a static member function, it can access only static data and can be directly called by using the scope resolution operator (::)

Some interesting facts about static member functions in C++
  • static member functions do not have this pointer.
  • A static member function cannot be virtual.
  • Member function declarations with the same name and the name parameter-type-list cannot be overloaded if any of them is a static member function declaration.
  • A static member function can not be declared const, volatile, or const volatile.
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *