Static Keyword in C++

Static in C++

Static keywords in C++  is used for Common space technique and sometimes scope modification in a C++ program. Static can be used in following contexts
  1. Static data members (Variables) 
    1. Inside a function
    2. Inside a class
  2. Static Objects of a class
  3. Static member functions
Static Data Members in C++

Static Data Members (Variables)

Inside a function

If a function has a static variable (data member), then the variable gets allocated throughout the lifetime of program. 

Calling the same function multiple times the variable will be allocated memory once only and value of the variable is retained in every function call for example :

Example

#include <iostream> 

using namespace std;

void PrepInsta()
{
// static variable, will only get initialised in first function call
static int count = 0;
cout << count << " ";

// value from previous function call is retained
// will be carried to next function calls
count++;
}

int main()
{
for (int i=0; i<10; i++)
PrepInsta();
return 0;
}

Output

0 1 2 3 4 5 6 7 8 9

Inside a class

Generally, separate copies &memories of variables (data members) are created for each object for any given class.

However, When a variable (data member) is declared as static only a single copy of memory is created for all the objects & each object shares the same copy.

Note – Static data members are class members not object member

Example

Static data members should be declared inside the class using the static keyword.

Syntax

static data_type var_name;

Example

static int a;//inside class

Static data members must be initialized outside the class using the scope resolution operator (::) operator.

Syntax:

data_type class_name::var_name=value;

Example:

int test::a=5;//outside class

Example Program

#include <iostream>
using namespace std;

class test
{
public:
static int i;//static data member declaration

test(){
/*static members of a class can't be initialised in a constructor
as they are not associated with each object of the class. It is shared by all objects.
*/
}
};

//static data members def must be outside the class
int test::i=1;

main()
{
//t1.i,t2.i is 1 now
test t1, t2;

cout << "t1.i=" << t1.i << endl;
cout << "t2.i=" << t2.i << endl;

// t2.i,t1.i also becomes 2
t2.i=2;

cout << "\nt1.i=" << t1.i << endl;
cout << "t2.i=" << t2.i << endl;
}

Output

t1.i=1
t2.i=1

t1.i=2
t2.i=2
  • Change in t2.i will also reflect the change in t3.i
  • As ‘i’ is static, only single copy is shared by all objects hence change in any object will have a change in all other objects.

Basic Properties of Static data members

  1. Static data members are initialized with zero automatically
  2. Static data members can be called directly using the class name
#include <iostream>
using namespace std;

class test
{
public:
static int i;//static data member declaration
};

//not defining but making accessible
int test::i;

main()
{
//object not created like test t1, t2

//static members can be accessed without created object
//just use name_Of_Class::variable_name
//if not declared static member's data value will be 0 by default
cout << "Value of static member = " << test::i << endl;

}

Output

Value of static member = 0

Static Member functions

Definition: A static member function is designed only for accessing static data members, other static member functions and any other functions from outside the class.

  • A static member function can be called even if no objects of the class exist
  • Static member functions have a class scope and do not have access to this pointer of the class.
  • Static function can only access static members of class

Example Program

/*Program to demonstrate static member functions*/

#include <iostream>
using namespace std;

class sample
{
public:
//static data members
static int a,b;

//non static data member
int c;

//static function definition
static
void add()
{
//static function can only access static data members
cout << "enter a,b values:";
cin >> a>>b;
cout << "a+b=" << a+b;

//error:cannot access non static data 'c' in static function
cin >> c;
}
};

//both values will become 0 by default
int sample::a;
int sample::b;

main()
{
//calling static function with out object
sample::add();
}

o/p

enter a,b:2 3
a+b=5

Static Objects

Very similar to static variables whose scope becomes till lifetime of the program, similarly, for static objects their scope becomes till just before the program terminates.

Example

Constructor functioning without static objects

#include <iostream>
using namespace std;

class PrepInsta 
{ 
    public: 
    PrepInsta() 
    { 
        cout << "PrepInsta Constructed\n"; 
    } 

    ~PrepInsta() 
    { 
        cout << "PrepInsta Destructed\n"; 
    } 
}; 

int main() 
{ 
    if (1) 
    {
        PrepInsta obj; //scope of obj starts here (Constructor called)

    }//scope of obj starts here (Destructor Called)

    cout << "End of main function\n"; 
}

Output

PrepInsta Constructed
PrepInsta Destructed
End of main function

Constructor functioning with static objects

#include <iostream>
using namespace std;

class PrepInsta 
{ 
    public: 
    PrepInsta() 
    { 
        cout << "PrepInsta Constructed\n"; 
    } 

    ~PrepInsta() 
    { 
        cout << "PrepInsta Destructed\n"; 
    } 
}; 

int main() 
{ 
    if (1) 
    {
        static PrepInsta obj; //scope of obj starts here (Constructor called)

    }

    cout << "End of main function\n"; 
//scope of obj starts here (Destructor Called) }
PrepInsta Constructed
End of main function
PrepInsta Destructed

Explanation

When static keyword is added to object, then rather than the scope of object being limited to the enclosed brackets it is in, the scope becomes till the program terminates.

Facts about static members / methods

  • Static data members can be accessed by other methods too
  • Overloading static methods is possible
  • Non-static data members can not be accessed by static methods.
  • Static methods can only access static members (data and methods)