Open In App
Related Articles

Static Objects in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Static Keyword in C++

An object becomes static when a static keyword is used in its declaration. Static objects are initialized only once and live until the program terminates. They are allocated storage in the data segment or BSS segment of the memory.

C++ supports two types of static objects:

  1. Local Static Objects
  2. Global Static Objects.

Syntax:

Test t;                // Stack based object
static Test t1;        // Static object

The first statement when executes creates an object on the stack means storage is allocated on the stack. Stack-based objects are also called automatic objects or local objects. The second statement creates a static object in the data segment or BSS segment of the memory.

Local Static Object

Local static objects in C++ are those static objects that are declared inside a block. Even though their lifespan is till the termination of the program, their scope is limited to the block in which they are declared.

Example:

C++

// C++ program to demonstrate the
//  usage of static keyword
#include <iostream>
class Test {
public:
    Test() { std::cout << "Constructor is executed\n"; }
    ~Test() { std::cout << "Destructor is executed\n"; }
};
  
void myfunc()
{
  // local static object declaration
    static Test obj;
}
  
int main()
{
    std::cout << "main() starts\n";
    myfunc(); // calling function with static object
    std::cout << "main() terminates\n";
    return 0;
}

                    

Output
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 goes out of scope because the local object obj is static with its lifetime till the end of the program. That is why its destructor will be called when main() terminates.

Now, what happens when we remove static in the above program?

As an experiment if we remove the static keyword from the global function myfunc(), we get the output shown below:

main() starts
Constructor is called
Destructor is called
main() terminates

This is because the object obj is now a stack-based object and it is destroyed when it goes out of scope and its destructor will be called.

Global Static Object

Global static objects are those objects that are declared outside any block. Their scope is the whole program and their lifespan is till the end of the program.

Example:

C++

// C++ program to demonstrate a global static keyword
#include <iostream>
class Test {
public:
    int a;
    Test()
    {
        a = 10;
        std::cout << "Constructor is executed\n";
    }
    ~Test() { std::cout << "Destructor is executed\n"; }
};
static Test obj;
int main()
{
    std::cout << "main() starts\n";
    std::cout << obj.a;
    std::cout << "\nmain() terminates\n";
    return 0;
}

                    

Output:

Constructor is executed
main() starts
10
main() terminates
Destructor is executed

We can see that the global static object is constructed even before the main function.

When are static objects destroyed?

As seen in both examples, static objects are always destroyed at the end of the program whether their scope is local or global. This property allows them to retain their value between multiple function calls and we can even use a pointer to them to access them out of scope.



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads