Question

bool SomeClass::Function( bool thankYou = true )
{

    static bool justAbool = false;
    // Do something with justAbool;
    ...
}

I have searched around but I can't find anything about this except globals vars or member functions itself.

What does the above do, i.e. what happens, does justAbool keep its value after leaving the scope? Or does it 'remember' the value when it re-enters the scope?

Was it helpful?

Solution

static when applied to a local variable gives that variable static storage duration. This means that the justAbool's lifetime lasts to the end of the program rather than to the end of the invocation of the function. It's scope stays the same, it can only be accessed by name in the function, after the declaration appears.

justAbool will be initialized (using the supplied initializer = false) the first time that the function is called. Thereafter it will retain its previous value, it will not be reinitialized when the function is called again.

Here are some fuller details about storage duration and lifetimes, with references to the standard.

If an object has static storage duration, it means that the storage for the object lasts for the duration of the program (beginning to end). (3.7.1 [basic.stc.static])

As a bool is a type without a non-trivial constructor, its lifetime mirrors that of its storage, i.e. it lives from the beginning to the end of the program. (3.8 [basic.life])

All objects with static storage duration (including local objects) are zero-initialized before any other initialization. (6.7/4 [stmt.decl]) [For local objects with an initializer this is fairly academic because there is no way to read their value before their declaration is reached.]

Local objects of POD type with static storage duration initialized with constant-expressions are initialized before their block is entered, otherwise local objects with static storage duration are initialized when control passes through their declaration. (6.7/4 again)

An implementation is permitter, but not required, to perform early initialization in some situations.

OTHER TIPS

The variable justAbool is initialized to false only once and it is initialized before the function is entered. The value will be remembered after leaving the scope of the function. It is important to note that the value will also be shared by all instances of SomeClass just like a static member variable. The variable justAbool will not be re-initialized if you create a new instance of your class and then call the function again.

The above function does what it does in the comment // Do something with justAbool;.

On a serious note, yes, the static variable (in this case justAbool) inside a function retains it's value even after returning from the function. It gets initialized ONLY ONCE. And each successive calls uses it as if it's a global variable. Its life-time is equal to the end of the program.

int f()
{
   static int v = 0;
   return ++v;
}
int main()
{
   cout << f() << endl;
   cout << f() << endl;
   cout << f() << endl;
   cout << f() << endl;
}

Output:

1
2
3
4

Online Demo : http://www.ideone.com/rvgB5

The justAbool is actually a regular static variable - it exists from the start of the program and is initialized only once. The special thing is that is is known only in this function - if you try and use it outside the function the compiler won't know what it is.

justAbool keeps its value after leaving the scope. What else did you want this code to do exactly?

function level static local variable, the initialization depends on variable types:

  • POD: initialized before main()
  • non-POD: initialized the first time, the line in the function is executed.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top