The static member functions have a class scope and they do not have access to the ‘this’ pointer of the class. When a member is declared as static that static member of class have only one such data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated.
Defining Static Function:
This is done by using the keyword static before the member function which is to be declared as static function.
static return_data_type function_name()
//Static function defined with keyword static
//Statements for execution inside static function
For example if a function exforsys returning nothing is to be declared as static function it is done as follows:
static void exforsys()
Accessing Static Function:
We have seen in our earlier sections about accessing a normal member function. To recollect, a normal member function gets accessed using the object and an operator called as the dot member access operator. The function declared as static or static functions gets accessed using just the class name and the operator called as scope resolution operator which is not possible in case of normal member functions.
Let us see an example to understand the declaration of static member function and how to access static member function in detail:
using namespace std;
static int sum; //Static data
example() //Constructor of the class
~example() //Destructor of the class
static void exforsys()
//Static function exforsys( ) defined with keyword static
cout << "nResult is: " << sum;
void number() //Normal member function number( )
cout << "nNumber is: " << x;
//Static function exforsys() accessed using class name example and the scope resolution operator ::
//Normal member function accessed using object e1 and the dot member access operator .
The output of the above program is:
In the above we have seen that the function exforsys() s defined as static function and the integer data type sum is declared as static data type. We see that four objects e1, e2, e3 and e4 are created for the class example. The constructor of the class example increments the sum by 1 and the destructor of the class decrements sum by 1.
We can see that the static function is accessed using the class name example and the scope resolution operator :: as
But the normal member function number() is accessed using the object name and the dot member access operator as
The first time when the static function exforsys() is called there was one object created and this the sum gets incremented by 1 in the constructor printing the result of sum as 1.When the static function exforsys() is called the second time there were three more objects e2,e3 and e4 created which makes the sum to gets incremented thrice from 1 in the constructor of the corresponding class namely example making the value of sum as 4 which is displayed in the second result. From the above explanation it is clear that the static function operate on the class and not in object generally. Also for accessing static function one can use the class name followed by the scope resolution operator as seen in example above.
One must note the following while using static member functions:
A static member function can access only static member data, static member functions and data and functions outside the class. So one must take essential care not to use static member function like a non-static member function which can access all of the above including the static data member.
A non-static member functions can be declared as virtual but care must be taken not to declare a static member function as virtual.
One must first understand the concept of static data also while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being public or private type in class definition. If a data is declared as static then the static data gets created and initialized only once. That is unlike non-static data members that are created again and again, for each separate object of the class the static data gets created and initialized only once. Just like the concept of static data, in which the variables are shared by all objects of the class in static functions also it apply to all objects of the class.
A non-static member function can be called only after instantiating the class as an object. But it is not the case with static member functions, because a static member function can be called, even when a class is not instantiated.
Also a static member function cannot have access to the ‘this’ pointer of the class.