Static member function
The key term static is used to precede the member function to create a member function static. A static function is a member function of class and the static member function can manipulate only on static data member of the class. A static member function acts as worldwide for member of its class without affecting the rest of the program. The purpose of static member is to decrease the requirement for global variables through giving alternatives which are local to a class. A static member function is not category of objects of a class. A Static member of a global class has external linkage. It does not have a this pointer so it can access nonstatic members of its class just by .(dot) or ->
The static member function cannot be a virtual function. This function cannot be declared along with the keyword const.
Example:
// describes static member function
# include<iostream.h>
class example
{
private:
static int count; //declaration of static data member
public:
example( );
static void display( );
};
//static data definition int example::count = 0;
example::example( )
{
++count;
}
void example::display( )
{
cout<<"counter value ="<< count << endl;
}
void main( )
{
cout<<"before instantiation "<< endl;
example::display ( );
example e1,e2;
cout<<"after instatntiation "<<endl;
example::display ( );
}
output is
before instantion
counter value =0
after instatiation
counter = 2