Sunteți pe pagina 1din 11

Static​ keyword has different meanings when used with different types.

We can use 
static keyword with: 
Static Variables :​ Variables in a function, Variables in a class 
Static Members of Class : C ​ lass objects and Functions in a class 
 
Static Variables 

● Static variables in a Function​: When a variable is declared as static, space for ​it 
gets allocated for the lifetime of the program​. Even if the function is called 
multiple times, space for the static variable is allocated only once and the value of 
variable in the previous call gets carried through the next function call.  
 
// C++ program to demonstrate the use of Static variables in a Function  
#include <iostream.h>  
#include <string.h>  
void demo()  

{  

// static variable  

static int count = 0;  

cout << count << " ";  

// value is updated and will be carried to next function calls  

count++;  

}  

int main()  

{  
for (int i=0; i<5; i++)   
demo();  
return 0;  
}  
 
Output : 
0 1 2 3 4
You can see in the above program that the variable count is declared as static. So, its 

value is carried through the function calls. The variable count is not getting initialized for 

every time the function is called. 

Static variables in a class​: As the variables declared as static are initialized only once 

as they are allocated space in separate static storage so, the static variables i​ n a class 

are shared by the objects.​ There can not be multiple copies of same static variables for 

different objects. Also because of this reason static variables can not be initialized 

using constructors. 

// C++ program to demonstrate static variables inside a class  

#include<iostream.h>  

class GG  

{  

public:  

static int i;  

GG()  

{  

// Do nothing  

};  

};  

int main()  

{  

GG obj1;  

GG obj2;  

obj1.i =2;  

obj2.i = 3;  

cout << obj1.i<<" "<<obj2.i;  // prints value of i  

}  
You can see in the above program that we have tried to create multiple copies of the 

static variable i for multiple objects. But this didn’t happen. So, a static variable inside a 

class should be initialized explicitly by the user using the class name and scope 

resolution operator outside the class as shown below: 

// C++ program to demonstrate static variables inside a class  

#include<iostream.h>  

class CDC  

{  

public:  

static int i;  

CDC()  

{  

// Do nothing  

};  

};  

int CDC::i = 1;  

int main()  

{  

CDC obj;  

// prints value of i  

cout << obj.i;  

}  

Output: 


Static Members of Class 

● Class objects as static​: Just like variables, objects also when declared as 
static have a scope till the lifetime of program. 
Consider the below program where the object is non-static. 
// CPP program to illustrate when not using static keyword  

#include<iostream.h>  

class CDC 
{  
int i;  
public:  
CDC()  
{  
i = 0;  
cout << "Inside Constructor\n";  
}  
~CDC()  
{  
cout << "Inside Destructor\n";  
}  
};  
int main()  
{  
int x = 0;  
if (x==0)  
{  
CDC obj;  
}  
cout << "End of main\n";  
}  
 
Output: 

Inside Constructor

Inside Destructor

End of main

In  the  above  program  the  object  is  declared  inside  the  if  block  as  non-static.  So,  the 
scope  of  variable  is  inside  the  if  block  only.  So  when  the  object  is  created  the 
constructor  is  invoked  and  soon  as  the  control  of  if  block  gets  over  the  destructor  is 
invoked as the scope of object is inside the if block only where it is declared. 
 
 
Let us now see the change in output if we declare the object as static. 

// CPP program to illustrate class objects as static  

#include<iostream.h>  

class G  

{  

int i = 0;  

public:  

G()  

{  

i = 0;  

cout << "Inside Constructor\n";  

}  

~G()  

{  

cout << "Inside Destructor\n";  

}  

};  

int main()  

{  

int x = 0;  

if (x==0)  

{  

static G obj;  

}  

cout << "End of main\n";  

}  
 

Output: 

Inside Constructor

End of main

Inside Destructor

● You can clearly see the change in output. Now the destructor is invoked 
after the end of main. This happened because the scope of static object is 
through out the life time of program. 
● Static functions in a class​: Just like the static data members or static 
variables inside the class, static member functions also does not depend on 
object of class. We are allowed to invoke a static member function using 
the object and the ‘.’ operator but it is recommended to invoke the static 
members using the class name and the scope resolution operator. 
Static member functions are allowed to access only the static data 
members or other static member functions​, they can not access the 
non-static data members or member functions of the class. 
// C++ program to demonstrate static member function in a class  

#include<iostream.h>  
class G  
{  
public:  
static void printMsg()  // static member function  
{  
cout<<"Welcome to CDC!";  
}  
};  
int main() // main function  
{  
G::printMsg();  // invoking a static member function 
}  
 
Output: 
Welcome to CDC!
Static data members in C++ 
#include <iostream.h>  
class A  
{  
public:  
A() { cout << "A's Constructor Called " << endl; }  
};  
 
class B  
{  
static A a;  
public:  
B() { cout << "B's Constructor Called " << endl; }  
};  
 
int main()  
{  
B b;  
return 0;  
}  
 
Output: 
B's Constructor Called

The above program calls only B’s constructor, it doesn’t call A’s constructor. The reason 

for this is simple, ​static members are only declared in class declaration, not defined. They 

must be explicitly defined outside the class using scope resolution operator​. 

If we try to access static member ‘a’ without explicit definition of it, we will get 

compilation error. For example, following program fails in compilation. 

#include <iostream.h>  

class A  

{  

int x;  

public:  

A()  

{ cout << "A's constructor called " << endl; }  

};  
class B  

{  

static A a;  

public:  

B()  

{ cout << "B's constructor called " << endl; }  

static A getA() 

{ return a; }  

};  

int main()  

{  

B b;  

A a = b.getA();  

return 0;  

}  

Output: 

Compiler Error: undefined reference to `B::a'


If we add definition of a, the program will works fine and will call A’s constructor. 

#include <iostream.h>  

class A  

{  

int x;  

public:  

A() { cout << "A's constructor called " << endl; }  

};  

class B  

{  

static A a;  
public:  

B() { cout << "B's constructor called " << endl; }  

static A getA() { return a; }  

};  

A B::a; // definition of a  

int main()  

{  

B b1, b2, b3;  

A a = b1.getA();  

return 0;  

}  

Output: 

A's constructor called

B's constructor called

B's constructor called

B's constructor called

Note that the above program calls B’s constructor 3 times for 3 objects (b1, b2 and b3), 

but calls A’s constructor only once. ​The reason is, static members are shared among all 

objects. That is why they are also known as class members or class fields. Also, static 

members can be accessed without any object​. 

#include <iostream.h>  
class A  
{  
int x;  
public:  
A() { cout << "A's constructor called " << endl; }  
};  
class B  
{  
static A a;  
public:  
B() { cout << "B's constructor called " << endl; }  
static A getA() { return a; }  
};  
A B::a; // definition of a  
int main()  
{  
// static member 'a' is accessed without any object of B  
A a = B::getA();  
return 0;  
}  
Output: 
A's constructor called

Some  interesting  facts  about  static  member  functions  in 


C++ 
1) static member functions do not have this pointer. 

For example following program fails in compilation with error ​“`this’ is unavailable for 

static member functions “ 

#include<iostream.h>  

class Test {   

static Test * fun() {  

return this; // compiler error  


}  
};  
 
int main()  
{  
getchar();  
return 0;  
}  
 
2)​ A static member function cannot be virtual  

3) Member function declarations with the same name and the name parameter-type-list 

cannot be overloaded if any of them is a static member function declaration. 

For example, following program fails in compilation with error “​‘void Test::fun()’ and 

`static void Test::fun()’ cannot be overloaded​ ” 


#include<iostream.h>  

class Test {  

static void fun() {}  

void fun() {} // compiler error  

};  

int main()  

{  

getchar();  

return 0;  

}  

4)​ A static member function can not be declared ​const​, v​ olatile​, or c


​ onst volatile​. 

For example, following program fails in compilation with error ​“static member function 

`static void Test::fun()’ cannot have `const’ method qualifier ” 

#include<iostream.h>  

class Test {   

static void fun() const { // compiler error  

return;  

}  

};  

int main()  

{  

getchar();  

return 0;  

}  

S-ar putea să vă placă și