Static Members of Class

Class objects as static: Just like variables, objects also when declared as static have a scope till the lifetime of the program. Consider the below program where the object is non-static. 

C++




// CPP program to illustrate
// when not using static keyword
#include <iostream>
using namespace std;
 
class GfG {
    int i;
 
public:
    GfG()
    {
        i = 0;
        cout << "Inside Constructor\n";
    }
    ~GfG() { cout << "Inside Destructor\n"; }
};
 
int main()
{
    int x = 0;
    if (x == 0) {
        GfG 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 a 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 the 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. 

C++




// CPP program to illustrate
// class objects as static
#include <iostream>
using namespace std;
 
class GfG {
    int i = 0;
 
public:
    GfG()
    {
        i = 0;
        cout << "Inside Constructor\n";
    }
 
    ~GfG() { cout << "Inside Destructor\n"; }
};
 
int main()
{
    int x = 0;
    if (x == 0) {
        static GfG 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 the main. This happened because the scope of static objects is throughout the lifetime of the program.

Static functions in a class: Just like the static data members or static variables inside the class, static member functions also do not depend on the object of the 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++




// C++ program to demonstrate static
// member function in a class
#include <iostream>
using namespace std;
 
class GfG {
public:
    // static member function
    static void printMsg() { cout << "Welcome to GfG!"; }
};
 
// main function
int main()
{
    // invoking a static member function
    GfG::printMsg();
}


Output

Welcome to GfG!

Related Articles:



Static Keyword in C++

Prerequisite: Static variables in C

The static keyword has different meanings when used with different types. We can use static keywords with:

  1. Static Variables: Variables in a function, Variables in a class
  2. Static Members of Class: Class objects and Functions in a class Let us now look at each one of these uses of static in detail.

Similar Reads

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 the variable in the previous call gets carried through the next function call. This is useful for implementing coroutines in C/C++ or any other application where the previous state of function needs to be stored....

Static Members of Class

...