Hello, folks! Today we will be unveiling another important aspect in the series of our C++ tutorials: Variants of static keyword in C++.
What does static keyword actually mean?
The static keyword
in C++ makes any variable, object or function a constant one. That means, only one copy of that particular variable or function would be created and will be accessed by all the instances of the class.
Thus, it would help the programmers assist and manage the memory in a better manner because only a single copy of the static variables/functions reside every time an object calls for it.
Variant 1: Static data members
If any data member or variable in C++ is declared as static, it acts like a class variable. Moreover, only one copy of the variable is available for the class objects to access.
The memory for the static variables get allocated only once, and further all the function which calls to the variable will available by the single copy of the variable. So, no instance copy of static variable is created.
Syntax: Static variable in a member function
1 |
static data_type variable = value; |
Syntax: Static variable within a Class
1 2 3 4 5 6 |
#Declaration of a static variable within a class static data_type variable; #Initializing a static variable within a class data_type class_name::variable=value; #Accessing the value of a static variable within a class Class_name.variable_name; |
Example 1: Static variable within a member function
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> #include <string> using namespace std; void count_static() { static int stat_var = 1; cout << stat_var <<'t'; stat_var=stat_var*2; } void count_local() { int loc_var = 1; cout << loc_var <<'t'; loc_var=loc_var*2; } int main() { cout<<"Manipulation on static variable:n"; for (int x=0; x<2; x++) count_static(); cout<<'n'; cout<<"Manipulation on local variable:n"; for (int y=0; y<2; y++) count_local(); return 0; } |
In the above example, we have done manipulations on a static and local variable to understand the difference between their functioning.
When we call the count_static() function, the value is once initialized and creates a single instance of the variable. When the main function encounters the count_static() function, the variable’s value travels through the function.
On the other hand, when we call the count_local() function two times in a loop, the variable will be initialized every time. Thus, the value of local variable remains 1.
Output:
1 2 3 4 |
Manipulation on static variable: 1 2 Manipulation on local variable: 1 1 |
Example 2: Static variable in a class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> using namespace std; class stat_var { public: static int var; int x; stat_var() { x=0; } }; int stat_var::var=10; int main() { stat_var V; cout << "Static Variable:"<<V.var; cout<<'n'; cout<<"Local Variable:"<<V.x; } |
In the above snippet of code, we have used a static variable within a class. We need to initialize the static variable outside the class, else an exception will be raised.
Further, we can access the value of the static variable using the class name. Thus, we no need to create an object for the same.
Output:
1 2 |
Static Variable:10 Local Variable:0 |
Variant 2: Static function in C++
Static member functions work with static data values and thus cannot access any non-static variables of the class.
Syntax: Static function definition
1 |
static return_type function_name(argument list) |
Syntax: Static Function call
1 |
Class_name::function_name(values) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> using namespace std; class ABC { public: static void stat_func(int a, int b) { static int res = a+b; cout<<"Addition:"<<res; } }; int main() { ABC::stat_func(10,30); } |
Output:
1 |
Addition:40 |
Important points regarding static member function
- Static member functions cannot be overloaded
- Any static member function cannot be virtual
- Static functions do not operate with this pointer
Conclusion
Thus, in this article, we have understood the working and variants of static keyword in C++.