<i id='BEdIm'><tr id='BEdIm'><dt id='BEdIm'><q id='BEdIm'><span id='BEdIm'><b id='BEdIm'><form id='BEdIm'><ins id='BEdIm'></ins><ul id='BEdIm'></ul><sub id='BEdIm'></sub></form><legend id='BEdIm'></legend><bdo id='BEdIm'><pre id='BEdIm'><center id='BEdIm'></center></pre></bdo></b><th id='BEdIm'></th></span></q></dt></tr></i><div id='BEdIm'><tfoot id='BEdIm'></tfoot><dl id='BEdIm'><fieldset id='BEdIm'></fieldset></dl></div>

    <small id='BEdIm'></small><noframes id='BEdIm'>

    • <bdo id='BEdIm'></bdo><ul id='BEdIm'></ul>
        <legend id='BEdIm'><style id='BEdIm'><dir id='BEdIm'><q id='BEdIm'></q></dir></style></legend>

        <tfoot id='BEdIm'></tfoot>

        什么时候分配/初始化函数级静态变量?

        时间:2023-05-24

            <tbody id='3Wfw7'></tbody>

            • <bdo id='3Wfw7'></bdo><ul id='3Wfw7'></ul>

                <small id='3Wfw7'></small><noframes id='3Wfw7'>

                  <legend id='3Wfw7'><style id='3Wfw7'><dir id='3Wfw7'><q id='3Wfw7'></q></dir></style></legend>
                • <i id='3Wfw7'><tr id='3Wfw7'><dt id='3Wfw7'><q id='3Wfw7'><span id='3Wfw7'><b id='3Wfw7'><form id='3Wfw7'><ins id='3Wfw7'></ins><ul id='3Wfw7'></ul><sub id='3Wfw7'></sub></form><legend id='3Wfw7'></legend><bdo id='3Wfw7'><pre id='3Wfw7'><center id='3Wfw7'></center></pre></bdo></b><th id='3Wfw7'></th></span></q></dt></tr></i><div id='3Wfw7'><tfoot id='3Wfw7'></tfoot><dl id='3Wfw7'><fieldset id='3Wfw7'></fieldset></dl></div>

                  <tfoot id='3Wfw7'></tfoot>
                • 本文介绍了什么时候分配/初始化函数级静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我非常有信心全局声明的变量在程序启动时被分配(并初始化,如果适用).

                  I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time.

                  int globalgarbage;
                  unsigned int anumber = 42;
                  

                  但是在函数中定义的静态函数呢?

                  But what about static ones defined within a function?

                  void doSomething()
                  {
                    static bool globalish = true;
                    // ...
                  }
                  

                  global 的空间是什么时候分配的?我猜程序什么时候开始.但是它也被初始化了吗?还是在第一次调用 doSomething() 时初始化?

                  When is the space for globalish allocated? I'm guessing when the program starts. But does it get initialized then too? Or is it initialized when doSomething() is first called?

                  推荐答案

                  我对这个很好奇所以写了下面的测试程序,并用g++ 4.1.2版本编译.

                  I was curious about this so I wrote the following test program and compiled it with g++ version 4.1.2.

                  include <iostream>
                  #include <string>
                  
                  using namespace std;
                  
                  class test
                  {
                  public:
                          test(const char *name)
                                  : _name(name)
                          {
                                  cout << _name << " created" << endl;
                          }
                  
                          ~test()
                          {
                                  cout << _name << " destroyed" << endl;
                          }
                  
                          string _name;
                  };
                  
                  test t("global variable");
                  
                  void f()
                  {
                          static test t("static variable");
                  
                          test t2("Local variable");
                  
                          cout << "Function executed" << endl;
                  }
                  
                  
                  int main()
                  {
                          test t("local to main");
                  
                          cout << "Program start" << endl;
                  
                          f();
                  
                          cout << "Program end" << endl;
                          return 0;
                  }
                  

                  结果出乎我的意料.直到第一次调用该函数时,才调用静态对象的构造函数.这是输出:

                  The results were not what I expected. The constructor for the static object was not called until the first time the function was called. Here is the output:

                  global variable created
                  local to main created
                  Program start
                  static variable created
                  Local variable created
                  Function executed
                  Local variable destroyed
                  Program end
                  local to main destroyed
                  static variable destroyed
                  global variable destroyed
                  

                  这篇关于什么时候分配/初始化函数级静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:c和c++上下文中静态、自动、全局和局部变量的区 下一篇:在 C++ 中使用表示变量名称的字符串访问变量值

                  相关文章

                  最新文章

                • <legend id='Dmg5Z'><style id='Dmg5Z'><dir id='Dmg5Z'><q id='Dmg5Z'></q></dir></style></legend><tfoot id='Dmg5Z'></tfoot>

                    <small id='Dmg5Z'></small><noframes id='Dmg5Z'>

                      <bdo id='Dmg5Z'></bdo><ul id='Dmg5Z'></ul>
                    1. <i id='Dmg5Z'><tr id='Dmg5Z'><dt id='Dmg5Z'><q id='Dmg5Z'><span id='Dmg5Z'><b id='Dmg5Z'><form id='Dmg5Z'><ins id='Dmg5Z'></ins><ul id='Dmg5Z'></ul><sub id='Dmg5Z'></sub></form><legend id='Dmg5Z'></legend><bdo id='Dmg5Z'><pre id='Dmg5Z'><center id='Dmg5Z'></center></pre></bdo></b><th id='Dmg5Z'></th></span></q></dt></tr></i><div id='Dmg5Z'><tfoot id='Dmg5Z'></tfoot><dl id='Dmg5Z'><fieldset id='Dmg5Z'></fieldset></dl></div>