
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When Are Static Objects Destroyed in C++
C++ Static Object
A static object is declared with the static keyword. The static objects are initialized only once and stored in the static storage area. The static objects are only destroyed when the program terminates i.e. they live until program termination.
In this article, we will understand static object, their types, and their examples.
Syntax of Static Object
The syntax for declaring a static object is as follows:
Animal cat; //object declaration static Animal dog; //static object declaration
Types of Static Objects
The static objects can be of two types, which are mentioned below:
1. Local Static Object
The local static objects are defined inside any block or function. The scope of the local static object is limited, but, the lifetime of the local static object is the same as lifetime of the program. It gets destroyed upon completion of the program. Here is an example of local static object.
Example
In this example, we have used a local static object to print the message inside the constructor. As we can see in the output, the statement of destructor is printed after last statement of the main() function is executed. This shows the scope of local static object is limited but the static object gets destroyed upon completion of the program.
#include <iostream> using namespace std; class Animal { public: Animal(){ cout << "There is a cat in constructor." << endl; } ~Animal() { cout << "Destructor being called.." << endl; } }; void obj (){ static Animal cat; } int main () { cout << "Entered the main function.." << endl; obj(); cout << "main function terminated..." << endl; return 0; }
The output of the above code is as follows:
Entered the main function.. There is a cat in constructor. main function terminated... Destructor being called..
2. Global Static Object
The global static objects are the objects whose scope is not limited to just a block or function. The scope of the global static object extends to complete program just like their lifespan. The global static objects gets destroyed only upon the completion of the program.
Example
In this example, we have used the global static object to print the message inside constructor. As we can see in the output, the message inside the constructor is printed even before the main() function is called and the message inside the destructor is printed after the last statement of main() function is executed. Here the object is created before main function and gets destroyed upon completion of program.
#include <iostream> using namespace std; class Animal { public: void fun(){ int a = 20, b = 30; cout << "Sum of " << a << " and " << b << " is: " << a+b << endl; } Animal(){ cout << "There is a cat in constructor." << endl; } ~Animal() { cout << "Destructor being called.." << endl; } }; static Animal cat; int main () { cout << "Entered the main function.." << endl; cat.fun(); cout << "main function terminated..." << endl; return 0; }
The output of the above code is as follows:
There is a cat in constructor. Entered the main function.. Sum of 20 and 30 is: 50 main function terminated... Destructor being called..