Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Static Functions

 

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.



Defining Static Function:

Static function is defined by using the keyword static before the member function that is to be declared as static function.


General syntax:



static return_data_type fucntionname()
//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.


Example:
The declaration of static member function and how to access static member function:



#include <iostream.h>
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;
}
};


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:


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


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.



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


  • A static member function can only access static member data, static member functions 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 the static 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 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.
    .
  • 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.

Read Next: C++ Pointers



 

 

Comments


XYZ ahanau said:

  Please add one more line after the class declaration:

int Base::sum=0;

then it will work fine.
November 18, 2008, 11:51 pm

Kanan said:

  No need of initiallizing a static variable as 0...
It is by default initiallized to zero when it is created...
March 20, 2009, 8:36 am

sss said:

  Hi!Why we need to declare static variable.
June 2, 2009, 6:32 am

vnd said:

  we use static when we want to share data that is not initialized every time we make an object and the current value can be used and updated by the objects, as in above example we can use it to keep track of number of objects created, the sum increses/decreases as objects created /destroyed.
June 17, 2009, 8:34 am

Rhizome said:

  We declare it outside the class so that there is only one copy of the variable for the class i.e objects
June 18, 2009, 1:12 am

Fahad said:

  When different objects are created only single copy of static data members and function is created which is shared among every object?
July 15, 2009, 5:54 pm

Seth said:

  My question is, why methods that are not members of any class are declared to be static?
July 20, 2009, 9:50 am

shri said:

  Why static member can access only static variable not other variables?
August 5, 2009, 2:48 am

ashitha said:

  Why it is called static member function?
August 9, 2009, 1:05 pm

Ganga said:

  Hi Shri,
Bcoz the static member function does not require the class instantiation.
August 17, 2009, 5:52 am

Ganga said:

  Hi Ashitha,
For all objects, the value of static variable is same. That is, only one value is maintained for all the objects created for that class.
August 17, 2009, 5:55 am

Sachin said:

  Static member data should always be initialized for C++ compiler.
once you declare static member variable , C++ Compiler only understand that variable has been declared and it need to be defined.it understand
return class name ::variable name = value
so always intialized static data member otherwise will get this error.

error LNK2001: unresolved external symbol "private: static int example::sum"
August 22, 2009, 7:51 am

Ali Hassan said:

  I wants to know about static function without using class member.
Means simple static function are what and why and how we use these static function in our main?
August 25, 2009, 9:54 pm

yes yes said:

  static funtions and data are just that. they are not linked to any particular object. for example if you had an Animal class and then in the main program you made instances of animals, but then u needed to know how many total animals have been created? this is achived by static data and static funtions which keep track of the overall state of the animal class! so:
class Animal{

static int numberOfAnimals = 0;
static void getTotalNumAnimals()
{
return numberOfAnimals;
}
Animal(int type)
{
numberOfAnimals += 1;
}

}


sortof thing:)

September 25, 2009, 7:54 am

sahana said:

  How can we create a class which keeps track of the number of its instances by using static data member
September 30, 2009, 2:37 pm

sudhansu said:

  What is the role of static in inheritance?
November 16, 2009, 2:51 am

sahil said:

  if we use normal function instead of static function.. will it give as the result (can access static data member), question if abt the above example.
December 22, 2009, 6:31 am

aqbal ahmad said:

  static member function can associate with the "this" pointer. why?
December 27, 2009, 12:48 am

Arvind said:

  If we use normal fuction instead of static function, will it give as the result [Can access static data member], question is about the above example.
January 4, 2010, 9:16 am

Atilla said:

  This code gives a link error as it has been said.
all static members should be defined at file scope (not in a member function).
i compiled the code and it gave an error.
please add the line:
int example::sum = 0;
outside the class deceleration and definition.

and of course, dont forget "using namespace std" directive.
January 5, 2010, 12:10 pm

Mani.Pullepudi said:

  Hello

static member function must be defined outside the class

untill and unless it was done so ,the code doesn't work.

befor the main function we need to add static member fn definition
January 26, 2010, 8:09 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape