Static is a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a function or class or with the static members of a class such as Class objects and Functions in a class.
When is used with any of the mentioned above, it cannot be modified in the future. static elements are allocated storage only once in a program lifetime.
Static Keyword can be used with the following in c++.
- Static variables in a functions
- Static Class Objects
- Static member Variable in class
- Static Methods in a class
1. Static variables in a Function
A variable declared static in a program is allocated to a static storage area. The static variables are only initialized only once in a program even if it is called multiple times in a program. It is useful when you do need to reset the counter even after multiple calls of the function.
Example of a static variable inside a function in c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; void counter() { // static variable static int count = 0; cout << count << " "; //updating counter but does not reset count++; } int main() { for (int i=0; i<=3; i++) counter(); return 0; } |
Output:
1 2 | //Output 0 1 2 3 |
2. Static class Objects
The class objects are declared as static works the same in the same way as static variables. The objects declared static are allocated to a static storage area and have a scope till the lifetime of the program.
Example of a static class object in c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; class Eg { int i; public: Eg() { i=0; cout << "CONSTRUCTOR" << endl; } ~Eg() { cout << "DESTRUCTOR" << endl; } }; int main() { int x = 0; if(x == 0) { static Eg eg; } cout << "END" << endl; } |
Output:
1 2 3 | CONSTRUCTOR END DESTRUCTOR |
As you can see the destructor is called after the end of the main function because the scope of static object is throughout the lifetime of the program.
3. Static Data Members inside a class:
Static variables of a class are shared by all the objects and these members are initialized once and allocated a separate single static storage. Each object does not have a separate copy of those variables.
Also, static data members are not initialized using the constructor as they are not dependent on variable initialization.
Example of a Static Data Members inside a class in c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<iostream> using namespace std; class Abc { public: static int x; //static member Abc() { // Do nothing }; }; int Abc::x = 1; int main() { Abc obj; obj.x = 10; // display x cout << obj.x; } |
Output:
1 2 | //Output 10 |
As you can see, it has to be explicitly initialized outside the class with a class name and scope resolution operator.
4. Static function in a class
Just like static data variables in a class, the static function also does not depend on the object of the class. We cannot call or invoke the static function of a class using object or ‘.
‘ operator. But can be invoked using the class name and the scope resolution operator.
However, these functions can only access the static data members and static member functions as it does not have ‘this‘ keyword.
Example of a Static function in a class in c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; class Abc { public: // static member function static void display() { cout<<"This is simple2code.com"; } }; int main() { //static member function invoked Abc :: display(); } |
Output:
1 2 | //Output This is simple2code.com |