简体   繁体   English

C ++ - 初始化中的静态分配

[英]C++ - Static Assignment in Initialization

Say I have a C++ function: 说我有一个C ++函数:

void foo(int x) {
    static int bar = x;
}

If I call foo(3) then afterwards call foo(4) , it is my understanding that the value of bar will still be 3. Why is this? 如果我调用foo(3)然后调用foo(4) ,我的理解是bar的值仍然是3.这是为什么? I understand why the memory allocation part of the initialization is redundant. 我理解为什么初始化的内存分配部分是多余的。 but why is the assignment also ignored? 但为什么这项任务也被忽略了?

It is not an "assignment". 这不是“任务”。 It is an initialization . 这是一个初始化 And, per rules of C++ language, initialization of static objects is performed only once - when the control passes over the declaration for the very first time . 并且,根据C ++语言的规则,静态对象的初始化只执行一次 - 当控件第一次传递声明

In your example, the control passes over the declaration of bar when x is 3 . 在您的示例中,当x3时,控件将传递bar的声明。 So, bar is initialized with 3 . 所以, bar3初始化。 It will never be "reinitialized", ie calling foo(4) will not affect bar at all. 它永远不会被“重新初始化”,即调用foo(4)根本不会影响bar If you want to change the value of bar after that, you have to modify bar directly. 如果要在此之后更改bar的值,则必须直接修改bar

Short answer: because the standard says so. 简短回答:因为标准是这么说的。

Long answer: that's not an assignment , but an initialisation , and it's ignored because the standard says so. 答案很长:这不是一个任务 ,而是一个初始化 ,它被忽略了,因为标准是这样说的。

The memory location for bar is not valid until the first call to foo , which is when bar gets instantiated. bar的内存位置在第一次调用foo之前无效,这是在实例化bar时。 bar gets initialized to x when it gets instantiated. bar在实例化时被初始化为x Every call to foo afterward, bar is already instantiated and therefore already initialized. 之后每次调用foobar都已经实例化,因此已经初始化。

this is initialization of static variable instead of assigning values to it. 这是静态变量的初始化,而不是为其赋值。 with that being said, the value of bar would always be 3 if u call foo(3) at the very first place. 话虽如此,如果你在第一个位置调用foo(3),bar的值总是为3。

Consider some different static variables: 考虑一些不同的静态变量:

void foo(int x) {
    static int bar = x;
    static std::string s1 = "baz";
    static std::string s2("baz");
    static int i{2};              // C++11-style uniform initialization
}

Do you also think s1 should get "assigned" the value "baz" every time the function is called? 您是否还认为每次调用函数时s1应该“赋值”值为"baz" What about s2 ? s2怎么样? What about i ? i呢?

None of those statements perform any assignment, they are all initializations, and they are only done once. 这些语句都不执行任何赋值,它们都是初始化,并且只执行一次。 Just because a statement includes the = character doesn't make it an assignment. 仅仅因为包含=字符的语句不能使其成为赋值。

A reason why the language is defined to work that way is that it's common to use a local static variable to run a function once: 为什么语言被定义为工作方式的一个原因是,它是共同使用本地静态变量运行一次的函数:

bool doInit()
{
  // run some one-time-only initialization code
  // ...
  return true;
}

void func() {
  static bool init = doInit();
  // ...
}

If init got assigned a value again every time the function is called then doInit() would get called multiple times, and would fail its purpose of running one-time-only setup. 如果每次调用函数时init再次分配一个值,那么doInit()将被多次调用,并且无法运行一次性设置。

If you want to change the value every time it's called, that's easy ... just change it. 如果你想在每次调用时更改值,那很简单......只需更改它即可。 But if you don't want it to keep changing then there would be no way to do that if the language worked the way you are asking about. 但是,如果你希望它不断变化那么就没有办法做到这一点,如果语言的运作方式则是问有关。

It would also be impossible to have static const local variable: static const局部变量也是不可能的:

void func() {
  static const bool init = doInit();
  // ...
}

Oops, this would try to change the value of init every time it's called. 糟糕,这会尝试在每次调用时更改init的值。

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

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