在不同范围内声明的静态变量之间的区别 [英] Difference between Static variable declared in different scopes

查看:87
本文介绍了在不同范围内声明的静态变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文件中的块内和块外声明静态变量有什么区别?例如,在这里,静态变量a,b,c,d之间有什么区别?我们可以声明一个可以从程序的所有文件访问的静态变量吗?

What is the difference between declaring static variable inside a block and outside a block in a file? Eg, here, what is difference between static variables a,b,c,d? Can we declare a static variable that is accessible from all files of a program?

    static int a;
    void getVol(..)
    { 
       static int b;
    }

    int main()
    {
       static int c;
       while(condition)
       {
           static int d;
            ....
       }
    some code here;
    return 0;
    }


推荐答案

最终,没有区别。 (暂时)忽略静态成员函数, static 表示含义;但是我们看到了在不同条件下含义的不同部分,因为某些含义也可以

Ultimately, there is no difference. Ignoring (for the moment) static member functions, static means what it means -- but we see different parts of what it means under different conditions because some of what it means can also happen without the keyword.

使用 static 关键字时,所定义的对象始终具有:

When you use the static keyword, the object being defined always has:


  1. 静态生存期–在程序的整个生命周期中都存在。

  2. 本地可见性-该名称在范围之外不可见

对于静态变量(无论是在块内还是在块外定义),这两者都是正确的。即使您不使用 static 关键字,默认情况下也会发生一部分或另一部分的更改,但是如果您使用关键字,则总是会得到两者。

Both of these are true about a static variable whether defined inside or outside a block. One part or the other will happen by default, even if you don't use the static keyword, but if you use the keyword, you always get both.

static 成员函数是类似的,但是由于它们是函数,因此它们并不完全具有生存期-所有函数都具有静态生存期。静态成员函数具有局部可见性(即,其名称仅在其类中可见),并且类似于静态生命周期。 -该函数未绑定到该类的实例。

static member functions are analogous, but since they're functions they don't exactly have lifetime -- all functions have static lifetime. A static member function has local visibility (i.e., its name is visible only with its class) and something sort of like "static lifetime" -- the function isn't bound to an instance of the class.

编辑:对于那些关心在何时初始化块级静态变量的人,请参见如下(第6.7 / 4节):

for those who care about the specific time at which a block-level static variable is intialized, the gory details are as follows (§6.7/4):


所有具有静态存储持续时间(3.7.1)的块范围变量的零初始化(8.5)。或线程存储持续时间(3.7.2)在执行任何其他初始化之前执行。如果适用,具有静态存储持续时间的块范围实体的常量初始化(3.6.2)在首次输入其块之前执行。

The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered.

允许实现对文件进行早期初始化其他具有静态或线程存储持续时间的块范围变量,且在允许实现对名称空间范围(3.6.2)中具有静态或线程存储持续时间的变量进行静态初始化的相同条件下。否则,该变量将在控件第一次通过其声明时进行初始化;这样的变量被认为是在其初始化完成时初始化的。

An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization.

因此,该变量将在程序启动的很早就被初始化为零。然后,如果指定了其他初始化,则将不迟于执行通过初始化时发生(但可能会更早)。但是请注意,常量初始化和其他初始化之间的区别。例如,考虑这样的事情:

So, the variable will be zero-initialized very early in the startup of the program. Then, if other initialization has been specified, that will happen no later than when execution passes through the initialization (but could happen earlier than that). Note, however, the difference between constant initialization and other initialization. Just for example, consider something like this:

int g()  { return 2; }

int f() { 
    goto bypass;

    static int x = 1;
    static int y = g();

bypass:

    std::cout << x << "\n" << y;
}

此处, x 是常量初始化的,但是 y 不是。由于 x 是常量初始化的,因此它在进入块时被初始化,因此,当我们打印其值时,应该得到 1 。但是, y 不是常量初始化的,因此goto表示执行过程永远不会经过初始化-因此,它将保留 0 在进行任何其他初始化之前对其进行了初始化,因此(使用运行正常的编译器)输出将是:

Here, x is constant initialized, but y is not. Since x is constant initialized, it is initialized upon entry to the block, so when we print out its value, we should get 1. y, however, is not constant initialized, and the goto means that execution never flows through its initialization -- therefore, it will retain the 0 that it was initialized with before any other initialization took place, so (with a properly functioning compiler) the output will be:

1 
0

这篇关于在不同范围内声明的静态变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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