Tuesday, May 25, 2010

C++ Static Functions

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.



static return_data_type fucntionname()//Static function defined with keyword static
{
statement1;
//Statements for execution inside static functionstatement2;
..........
..........
}

For example if a function sys1 returning nothing is to be declared as staic function it is done as follows:

static void sys1()
{
........;
.......;
}


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.

#include
class example
{
private:
static int sum;            
//Static dataint x;
public:
example()                  
//Constructor of the class{
sum=sum+1;
x=sum;
}


~example()                //Destructor of the class{
sum=sum-1;
}


static void sys1()       //Static function exforsys( ) defined with keyword static{
cout<<"\nResult is: "<

}


void number()            //Normal member function number( ){
cout<<"\nNumber is: "<

}
};


void main()
{
example e1;
example::sys1();
//Static function exforsys() accessed using class name example and the scope resolution operator ::example e2,e3,e4;
example::sys1();
e1.number();
//Normal member function accessed using object e1 and the dot member accessoperator.
e2.number();
e3.number();
e4.number();
}



The output of the above program is:


Result is: 1
Result is: 4
Number is: 1
Number is: 2
Number is: 3
Number is: 4

The programmer must note the following while using static member functions:


  • A static member function can only access static member data, static memberfunctions and data and functions outside the class. The programmer 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 thestatic data member. .
  • A non-static member function can be declared as virtual but care must be taken not to declare a static member function as virtual. .
  • 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 asstatic 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 ofstatic data, all objects of the class in static functions share the variables. This applies to all objects of the class. .
  • 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. .
  • A static member function cannot have access to the 'this' pointer of the class.
reference exforsys.com





No comments:

Post a Comment