static var in member function

北城以北 提交于 2019-12-06 02:15:29

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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




回答6:


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.


来源:https://stackoverflow.com/questions/4977290/static-var-in-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!