简体   繁体   English

如果我声明并初始化 static 变量会发生什么? 每次调用都会设置它的值吗?

[英]what happens if I declare and initialize a static variable? Will its value be set on every call?

Let's say I have the following piece of code:假设我有以下代码:

void foo(){
    static int bar = 0;
    bar++;
}

Does two successive calls to this function reset the value of the variable?对此 function 的两次连续调用是否会重置变量的值?

Initialization occurs when an object is created.在创建 object 时进行初始化。 For static objects, their lifetimes start when program execution starts.对于 static 对象,它们的生命周期从程序执行开始时开始。 They are not reinitialized or reassigned when execution reaches the statements that define them.当执行到达定义它们的语句时,它们不会重新初始化或重新分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM