Tutorials
C++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 function is defined by using the keyword static before the member function that is to be declared as static function.
General syntax:
|
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:
|
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:
|
Please add one more line after the class declaration: int Base::sum=0; then it will work fine. |
|
No need of initiallizing a static variable as 0... It is by default initiallized to zero when it is created... |
| Hi!Why we need to declare static variable. |
| 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. |
| We declare it outside the class so that there is only one copy of the variable for the class i.e objects |
| When different objects are created only single copy of static data members and function is created which is shared among every object? |
| My question is, why methods that are not members of any class are declared to be static? |
| Why static member can access only static variable not other variables? |
| Why it is called static member function? |
|
Hi Shri, Bcoz the static member function does not require the class instantiation. |
|
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. |
|
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" |
|
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? |
|
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:) |
| How can we create a class which keeps track of the number of its instances by using static data member |
| What is the role of static in inheritance? |
| 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. |
| static member function can associate with the "this" pointer. why? |
| 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. |
|
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. |
|
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 |