- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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++?
Static objects are declared with the keyword static. They 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.
A program that demonstrates static objects in C++ is given as follows.
Example
#include <iostream> using namespace std; class Base { public : int func() { int a = 20; cout << "The value of a : " << a; } }; int main() { static Base b; b.func(); return 0; }
Output
The output of the above program is as follows.
The value of a : 20
Now let us understand the above program.
The function func() in class Base declares an int variable a and then displays the value of a. The code snippet that shows this is as follows.
class Base { public : int func() { int a = 20; cout << "The value of a : " << a; } };
In the function main(), a static object b is created of class Base. Then function func() is called. Since object b is static, it is only destroyed when the program terminates. The code snippet that shows this is as follows.
int main() { static Base b; b.func(); return 0; }
- Related Articles
- When are static C++ class members initialized?
- When and where static blocks are executed in java?
- Count the number of objects using Static member function in C++
- How to call a static constructor or when static constructor is called in C#?
- Count the number of objects using Static member function in C++ Program
- When do function-level static variables get initialized in C/C++?
- Where are static variables stored in C/C++?
- When are python objects candidates for garbage collection?
- What are objects in C#?
- What are static member functions in C#?
- Give reasons for the following:Blue colour of copper sulphate solution is destroyed when iron fillings are added to it.
- Are values returned by static method are static in java?
- What are the differences between a static and a non-static class in C#?
- Static vs. Non-Static method in C#
- Are the values of static variables stored when an object is serialized in Java?
