Demonstrate that statics are only constructed once - C++ Class

C++ examples for Class:Constructor

Description

Demonstrate that statics are only constructed once

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class DoNothing//from  w  w w. j a va 2s . c o m
{
  public:
    DoNothing(int initial) : nValue(initial)
    {
        cout << "DoNothing constructed with a value of " << initial << endl;
    }
    ~DoNothing()
    {
        cout << "DoNothing object destructed" << endl;
    }
    int nValue;
};
void fn(int i)
{
    cout << "Function fn passed a value of " << i << endl;
    static DoNothing dn(i);
}

int main(int argcs, char* pArgs[])
{
    fn(10);
    fn(20);

    return 0;
}

Result


Related Tutorials