6
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?

Adrian W
  • 4,563
  • 11
  • 38
  • 52
  • 3
    It is not scoop. It is scope. – Arjun J Rao Feb 12 '11 at 09:22
  • 1
    @Arjun J Rao; thanks for the correction –  Feb 12 '11 at 09:27
  • @All others; can only state one answer as accepted. So I choose the first. Thanks. –  Feb 12 '11 at 09:48
  • @Xeo; Will type better next time, oops. Though I did type scoop and not scope. As I thought it was called like that. So changed it back due to the comments thereafter. Disliking that kind of edit-abillity a bit, they are my words not yours. Won't make me popular propably but hey. Anyway thanks. –  Feb 12 '11 at 10:19

6 Answers6

27

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.

Adam
  • 389
  • 1
  • 4
  • 8
  • 9
    Very good point on value shared by all instances. It's not obvious. – Boon Dec 30 '14 at 01:16
  • 1
    @Adam: Yes I was also confused whether the static local variable inside member function would be shared by all instances of the class. Now that we know I wonder why would someone declare and define static variables of a class inside a member function and not inside and header/cpp file in the usual way – nurabha Jun 09 '15 at 13:54
  • 1
    @nurabha The most likely reason for a non-static member function to have a local static variable is to limit that variable's visibility to only that function, and prevent it from cluttering the class' interface. If the data needs to be shared by all instances of the class, but is only required by that one function, then a local static variable will indicate this. Unfortunately, the fact that it's shared by all instances is non-obvious, so making the variable a `private` static class member would communicate intent more clearly, but make it possible for other members to inadvertently modify it. – Justin Time - Reinstate Monica Apr 27 '17 at 18:53
12

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

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

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    What does "Ifs life-time is equal to the end of the program" mean? A lifetime is usually a duration, not a point in time. – CB Bailey Feb 12 '11 at 10:02
0

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.
pepero
  • 7,095
  • 7
  • 41
  • 72
0

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.

Eli Iser
  • 2,788
  • 1
  • 19
  • 29
  • @Eli: It is not necessarily true *"it exists from the start of the program"*.. and most likely is not true. hence -1 – Nawaz Feb 12 '11 at 09:46
  • @Nawaz: That's what static storage duration means. That is one ill-informed -1. – CB Bailey Feb 12 '11 at 09:48
  • @Charles: It gets initialized when the function is called first time, right? – Nawaz Feb 12 '11 at 09:49
  • @Charles: Yeah. That is exactly what I meant. Edited my previous comment! – Nawaz Feb 12 '11 at 09:51
  • @Charles - this is not correct, there is no difference between a static variable that is outside a function and one that is inside a function. Both can be initialized only with a constant value, and do this at the start of the program (the initialization done before `main`). The only difference is the scope of reference. – Eli Iser Feb 12 '11 at 09:53
  • @EliIser: OK, strictly speaking, POD types initialized with constant expressions are initialized before their containing block is first entered. It's a pretty picky distinction though. – CB Bailey Feb 12 '11 at 09:57
  • @Nawaz: I think that you are confusion initialization and storage duration. The storage for the object exists for the entire duration of the program. As it is a POD-type it means that the object lives for the entire duration of the program. It is only zero-initialized until the block which contains its declaration is first executed when the given initializer is acutally used, but that is a separate concern. – CB Bailey Feb 12 '11 at 09:59
  • @Charles: I think you're right. I'm reverting my downvote. +1 now. – Nawaz Feb 12 '11 at 10:49
0

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

Arjun J Rao
  • 925
  • 1
  • 10
  • 25
  • it's modified from a source - narrowed down to basic - and I didn't knew statics could be in a function scope too. So I wondered what is happenening. msdn wasn't real clear too me and couldn't find anything else around the net. As it is propably a general question I thought this is the best place. That's practically what I want this code to do. –  Feb 12 '11 at 09:43