Remain Careful from these two persons
new friends and old enemies — Kabir
What is static keyword in C++?
static keyword can be applied to local variables, functions, class’ data members and objects in C++. static local variable retain their values between function call and initialized only once. static function can be directly called using the scope resolution operator preceded by class name (See this, this and this for more details). C++ also supports static objects.
What are static objects in C++?
An object become static when static keyword is used in its declaration. See the following two statements for example in C++.
Test t; // Stack based object static Test t1; // Static object
First statement when executes creates object on stack means storage is allocated on stack. Stack based objects are also called automatic objects or local objects. static object are initialized only once and live until the program terminates. Local object is created each time its declaration is encountered in the execution of program.
static objects are allocated storage in static storage area. static object is destroyed at the termination of program. C++ supports both local static object and global static object
Following is example that shows use of local static object.
main() starts Constructor is executed main() terminates Destructor is executed
If we observe the output of this program closely, we can see that the destructor for the local object named obj is not called after it’s constructor is executed because the local object is static so it has scope till the lifetime of program so it’s destructor will be called when main() terminates.
What happens when we remove static in above program?
As an experiment if we remove the static keyword from the global function myfunc(), we get the output as below:
main() starts Constructor is called Destructor is called main() terminates
This is because the object is now stack based object and it is destroyed when it is goes out of scope and its destructor will be called.
How about global static objects?
The following program demonstrates use of global static object.
Constructor is executed main() starts 10 main() terminates Destructor is executed
This article is contributed by Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count the number of objects using Static member function
- Understanding storage of static methods and static variables in Java
- Understanding "static" in "public static void main" in Java
- Why non-static variable cannot be referenced from a static method in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- Internal static variable vs. External static variable with Examples in C
- Static functions in C
- Static Keyword in C++
- Static Variables in C
- Static import in Java
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 6
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 1
- Templates and Static variables in C++
- Can static functions be virtual in C++?
- Initialization of static variables in C
- Initialize a static Map using Java 9 Map.of()