My 2 Cents Static Variables

Swati Mishra

Content Writer
We know that each object has its own set of member variables. In some situations, it is preferable to have one or more variables that are common for all objects. So, the static variable provide buy C++ for this.
Static variables retain their value even after the function to which it belongs has been executed.
The static variables have to be initialized explicitly either inside the class declaration or outside the class declaration.
Syntax

Class staticname
{
Int data;
Static int staticVar; // Static variable declared
};
Int staticname :: staticVar=0; // static variable initialized to 0
 

VIVEK KUMAR

White Belt
You are right , I am going to add some another important point regarding static variable -
For static variable memory allocate done at the compile time.
By default static variable initialize to zero.
Its scope and life remain throughout the program.
It does not support the recursion.
 

Swati Mishra

Content Writer
You are right , I am going to add some another important point regarding static variable -
For static variable memory allocate done at the compile time.
By default static variable initialize to zero.
Its scope and life remain throughout the program.
It does not support the recursion.
and it is easy for that you declare or outside the class or also can declare inside the class.
 

Doominic anderson

White Belt
Static variables declared static within the body of a function maintains its value between invocation pf the function. A variable declared static within a module is accessible by all the functions within that module,however it is not accessible by functions from other modules. Static variables exist as member of the class rather than as an instance in each object of the class. There is only a single instance of each static data member for the entire class.
 

Manish Mishra

Content Writer
Static variable are the variable that once declared initiated to undefined value and remain static within the same class. Using Static functions it is declared in the programme.
 
Top