I was reading c++ code. I saw a static variable has been used as a counter and it has been initialized in the constructor as below :
class Order{
int Number;
public:
Order(){
int static i=0;
Number=++i;
}
int getNumber(){
return Number;
}
.
.
So if we instantiate the "Order" class as below :
Order *o1 = new Order();
o1.getNumber();
Order *o2 = new Order();
o2.getNumber();
Order *o3 = new Order();
o3.getNumber();
//Result : 1 ,2 ,3
I am wondering how the static variable work in this case. Because each time we instantiate the Order class, actually we are setting int static i=0; so I expect a result like this :
1,1,1
but it seems the process behind static variables is different! So, what is going on behind this static variable and how it works?