成员函数中的静态变量 [英] static var in member function

查看:162
本文介绍了成员函数中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Greets

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

static bool justAbool = false;
//使用justAbool做一些事情
...
}

搜索,但找不到



上面做的是什么,即发生了什么,justAbool在离开scoop之后保持它的值吗?



谢谢

解决方案

static 当应用于本地变量时,赋予该变量静态存储持续时间。这意味着 justAbool 的生命周期持续到程序结束,而不是调用函数的结束。



justAbool 将在第一次调用函数时初始化(使用提供的初始化器 = false )。



这里有一些关于存储持续时间和寿命的更详细的细节,引用了

如果对象具有静态存储持续时间,则意味着对象的存储持续了程序的持续时间(开始到结束)。 (3.7.1 [basic.stc.static])



由于 bool 琐碎的构造函数,它的生命反映了它的存储,即它从程序的开始到结束。 (3.8 [basic.life])



具有静态存储持续时间(包括本地对象)的所有对象在任何其他初始化之前都进行初始化。 (6.7 / 4 [stmt.decl])[对于具有初始化器的本地对象,这是相当学术的,因为在到达它们的声明之前没有办法读取它们的值。]



在输入块之前初始化具有用常量表达式初始化的静态存储持续时间的POD类型的本地对象,否则具有静态存储持续时间在控制通过它们的声明时被初始化。 (再次为6.7 / 4)



某些实现允许在某些情况下执行早期初始化,但不是必需的。


Greets

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

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

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

What does the above do, i.e. what is happening, does justAbool keep its value after leaving the scoop? Or does it 'remember' the value again when entering the scoop again?

Thank you

解决方案

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.

这篇关于成员函数中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆