Broad Network


Static Objects in C++

C++ Just After the Basics - Part 10

Forward: In this part of the series, I explain the meaning of static objects in C++.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

This is part 10 of my series, C++ Just After the Basics. In this part of the series, I explain the meaning of static objects in C++.

Scope
Any normal object is destroyed by C++ when it goes out of scope. A static object is an object that is not destroyed when it goes out of scope. Also, with static objects, if the scope is executed repeatedly, initialization takes place only once, the first time, the scope is executed. I will use the function scope to explain this. I will start with a normal object in a function and shows how it dies when it goes out of scope. Then I will define a static object in the same function and prove that it does not die, when it goes out of scope.

Normal Object
Consider the function, fn() in the following code:

#include <iostream>
using namespace std;

void fn()
    {
        int n = 0;

        cout << n << '\n';

        n = n + 2;

    }


int main()
    {
        fn();
        fn();
        fn();
        fn();

        return 0;
    }

In the function, fn(), n is an ordinary integer object. In main, the function fn() is called 4 times. Each time the function, fn() in main is called, n is reinitialized to zero.  So the value displayed for n from fn() is always zero. After the display (cout statement), n is increased by 2, but this has no effect in the display, the next time fn() is called. This increase of n and n as a whole, die (is annulled) at the very end of the function block.

Static Object
If in the fn() function above, n where made static, then n will be initialized only when the function is called for the first time. That is one of the characteristics of a static object. Another characteristic of a static object is that it does not die when it goes out of scope. So, for a static object, if the scope is re-executed, the previous value of the static object will be maintained. To define a static object, precede the normal definition with the reserved word, static. Read and try the following code:

#include <iostream>
using namespace std;

void fn()
    {
        static int n = 0;

        cout << n << '\n';

        n = n + 2;

    }


int main()
    {
        fn();
        fn();
        fn();
        fn();

        return 0;
    }

In the fn() function, n is now a static object. The first time the fn() function is called from main, n is initialized to zero. Then n is displayed. Then n is increased by 2. and the function block goes out of scope. Since the value of a static object does not die when it goes out of scope, the last value of n is retained by n.

The next time, the function, fn(),  is called, n is no longer initialized; that is, the initialization statement is skipped. After the skipping, n is displayed, and then n is increased by 2 on its previous value. So, the output of the above code is, 0, 2, 4, 6, in new lines.

So, a static object is an object that is initialized only once if its scope block is executed many times and it is an object that does not die when it goes out of scope.

The initialization of a static object should not be broken down into definition and assignment.

Not seen Out of Scope
A static object cannot be seen out of its scope. For example, in the above program, if you use the identifier, n outside the fn() function block, the compiler will not compile the program. So the following program in which n has been used outside its scope, is not compiled.

#include <iostream>
using namespace std;

void fn()
    {
        static int n = 0;

        cout << n << '\n';

        n = n + 2;

    }

n = 15;  //used outside scope

int main()
    {
        fn();
        fn();
        fn();
        fn();

        return 0;
    }

That is what I have prepared for static objects. I hope you appreciated it.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message