Introduction
This article talks about Static in C++.
Background
Static Variables:
1. All static variables are stored in Data Segement.
2. Static variables are local in scope to their module in which they are defined, but
their life time is throughout the program.
3. All Static variables are by default intiliased by the compiler as "0", unless they are
explicitly initialised or modified with a different value.
4. Static attributes are not considered while calculating the size of a class object.
Static Global Variables:
The Global varaible declared with Static specifier is visible at the file scope. And is accessible only in the file where it is defined.
Static Local variables in a function:
1. The scope is only to that function.
2. Here, the Initialization is performed only at the first call to a function and data is
retained between calls to that function.
3. We can have same named static variables in 2 different functions and are
differntiated by their name that is stored in Data segment. Compler does name
mangling to differentaite them.
Usage of static variables in a function:
The code
Code: Cpp
int func()
{
static int var; //initialized to 0 by default
return (++ var);
}
Static in a class:
1. We can have Static member variables and Static member functions in a class.
2. Variables or functions do not require an instance of the class to exist.
3. One copy of this variable/function is shared between all the instances of the class.
They are normally accessed using the <class name> with scope operator ::
Static Member Variables in a class:
1. Also known as class varibles.
2. We normally access the static variables as below:
<class_name>::a ;
3. We cannot initialise the static members inside the class declartion. So, if we
declare a class in a .h file, usually initialization is done in the respective .cc file.
Syntax to initialize the static member variable is
type <class_name>::<static_variable> = value ;
Example:
The code
Code: Cpp
static4.h file
-------------
#include<iostream.h>
class Abc
{
static int count;
public:
static void incr();
};
static4.cc file
---------------
#include "static4.h"
int Abc::count=0;
void Abc::incr()
{
++ count;
cout<<"Static variable value:"<<Abc::count<<"\n";
}
int main()
{
//calling static function of Abc
Abc::incr();
return(0);
}
Output:
Static variable value:1
Here it sounds that private varible count is accessible from outside the class, but here we are only defining and initiazing it and not really accessing it.
4. We can initialize constant static variables inside the declaration of the class
Example:
The code
Code: Cpp
#include<iostream.h>
class Abc
{
//initializing the static variable count
static const int count = 80;
public:
static void display();
};
void Abc::display()
{
cout<<"Static constant variable value is :"<<Abc::count<<"\n";
}
int main()
{
//calling static function of Abc
Abc::display();
return(0);
}
Output:
Static constant variable value is :80
But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example :
const int Abc::count[3] = {80,90,100};
Static Member Functions in a class:
1. They can only operate on static member variables.
2. A static member function does not have a this pointer.
3. Non-Static member functions can call Static Member functions using this pointer.
Example:
The code
Code: Cpp
#include<iostream.h>
class Abc
{
public:
void display();
private:
static void func();
};
void Abc::func()
{
cout<<"my name is Mridula ...\n";
}
void Abc::display()
{
this->func(); //calling static function
}
int main()
{
Abc abc;
abc.display();
return (0);
}
Output:
my name is Mridula ...
4. You can call non-static member functions of a class from a Static Member function
using reference or pointers to that class object.
Example:
The code
Code: Cpp
#include<iostream.h>
class Abc
{
public:
Abc(){};
~Abc(){};
void print();
static void func(Abc* abc);
};
void Abc::func(Abc* abc)
{
abc->print();
}
void Abc::print()
{
cout<<"My name is Mridula...\n";
}
int main()
{
Abc *abc = new Abc;
Abc::func(abc);
delete (abc);
return(0);
}
Output:
My name is Mridula...
5. Static member functions are inherited but can not be overidden.
But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below:
The code
Code: Cpp
#include<iostream.h>
class Base
{
static void func();
public:
Base(){};
~Base(){};
virtual void print();
};
class Derived: public Base
{
static void func();
public:
Derived(){};
~Derived(){};
void print();
};
void Base::func()
{
cout<<"Base::My name is Mridula...\n";
}
void Base::print()
{
func();
}
void Derived::print()
{
func();
}
void Derived::func()
{
cout<<"Derived::My name is Mridula...\n";
}
int main()
{
Base *abc = new Derived;
abc->print();
delete (abc);
return(0);
}
Output:
Derived::My name is Mridula...
thanks
Mridula.



