Topics
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, a static member of class, it has only one 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
Static function is defined by using the keyword static before the member function that is to be declared as static function.
Syntax
static return_data_type function_name()
//Static function defined with keyword static
{
statement1;
//Statements for execution inside static function
statement2;
...
...
}
For example if a function exforsys returning nothing is to be declared as staic function it is done as follows:
static void exforsys()
{
... ;
... ;
}
Accessing Static Function
A normal member function is accessed using the object and an operator called the dot member access operator. The functions declared static or static functions are accessed using only the class name and the scope resolution operator, unlike in normal member functions where these are not used.
Static Function Example
The declaration of static member function and how to access static member function:
#include <iostream>
using namespace std;
class example
{
private:
static int sum; //Static data
int x;
public:
example() //Constructor of the class
{
sum=sum+1;
x=sum;
}
~example() //Destructor of the class
{
sum=sum-1;
}
static void exforsys()
//Static function exforsys( ) defined with keyword static
{
cout << "nResult is: " << sum;
}
void number() //Normal member function number( )
{
cout << "nNumber is: " << x;
}
};
int example::sum=0;
void main()
{
example e1;
example::exforsys();
//Static function exforsys() accessed using class name example and the scope resolution operator ::
example e2,e3,e4;
example::exforsys();
e1.number();
//Normal member function accessed using object e1 and the dot member access operator.
e2.number();
e3.number();
e4.number();
}
The output of the above program is:
In the above example, the function exforsys() is defined as static function and the integer data type sum is declared as static data type. 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.
The static function is accessed using the class name example and the scope resolution operator :: as
example::exforsys();
But the normal member function number() is accessed using the object name and the dot member access operator as
e1.number()
e2.number()
e3.number()
e4.number()
The first time the static function exforsys() is called, there was one object created and thus, the sum is 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 results in the sum incremented thrice from 1 in the constructor of the corresponding class example, resulting in the value of sum as 4, which is displayed in the second result. Applying the above explanation, it is clear that the static function operates on the class and not in object. To access static function the programmer can use the class name, followed by the scope resolution operator, as seen in example above.
Things to keep in mind while using static member functions
1. A static member function can only access static member data, static member functions and data and functions outside the class.
2. You must take note not to use static member function in the same manner as non-static member function, as non-static member function can access all of the above including the static data member.
3. A non-static member function can be declared as virtual but care must be taken not to declare a static member function as virtual. v
4. The programmer must first understand the concept of static data while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once. Non-static data members are created again and again. For each separate object of the class, the static data is created and initialized only once. As in the concept of static data, all objects of the class in static functions share the variables. This applies to all objects of the class.
5. A non-static member function can be called only after instantiating the class as an object. This is not the case with static member functions. A static member function can be called, even when a class is not instantiated.
6. A static member function cannot have access to the ‘this’ pointer of the class.