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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Internal static variable vs. External static variable with Examples in C
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Understanding "static" in "public static void main" in Java
- Count the number of objects using Static member function
- Static functions in C
- How are variables scoped in C - Static or Dynamic?
- Can static functions be virtual in C++?
- What are the default values of static variables in C?
- Templates and Static variables in C++
- Comparison of static keyword in C++ and Java
- Initialization of static variables in C
- Static data members in C++
- Initialization of global and static variables in C
- Static Variables in C
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3