• <small id='n7gZx'></small><noframes id='n7gZx'>

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

        什么时候初始化静态 C++ 类成员?

        时间:2023-12-03

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

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

                  <bdo id='Mf4FX'></bdo><ul id='Mf4FX'></ul>

                  本文介绍了什么时候初始化静态 C++ 类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对此似乎没有简单的答案,但是否可以安全地做出任何关于何时可以访问静态类字段的假设?

                  There appears to be no easy answer to this, but are there any assumptions that can be safely made about when a static class field can be accessed?

                  唯一安全的假设似乎是所有静态都在程序开始之前初始化(调用main).那么,只要我不从其他静态初始化代码中引用静态,我就没有什么可担心的吗?

                  The only safe assumption seems to be that all statics are initialized before the program commences (call to main). So, as long as I don't reference statics from other static initialization code, I should have nothing to worry about?

                  推荐答案

                  该标准保证了两件事 - 在同一个翻译单元(通常意味着 .cpp 文件)中定义的对象按其定义的顺序进行初始化(不是声明):

                  The standard guarantees two things - that objects defined in the same translation unit (usually it means .cpp file) are initialized in order of their definitions (not declarations):

                  3.6.2

                  具有静态存储持续时间 (basic.stc.static) 的对象的存储应在任何其他初始化发生之前进行零初始化 (dcl.init).零初始化和用常量表达式初始化统称为静态初始化;所有其他初始化都是动态初始化.使用常量表达式 (expr.const) 初始化静态存储持续时间的 POD 类型 (basic.types) 对象应在任何动态初始化发生之前进行初始化.在同一个翻译单元的命名空间范围内定义静态存储持续时间并动态初始化的对象,应按照其定义在翻译单元中出现的顺序进行初始化.

                  The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (basic.types) with static storage duration initialized with constant expressions (expr.const) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.

                  另一个有保证的事情是,在使用来自该翻译单元的任何对象或函数之前,将完成来自翻译单元的静态对象的初始化:

                  The other guaranteed thing is that initialization of static objects from a translation unit will be done before use of any object or function from this translation unit:

                  命名空间范围的对象的动态初始化(dcl.init、class.static、class.ctor、class.expl.init)是否在main的第一条语句之前完成是实现定义的.如果初始化推迟到 main 的第一条语句之后的某个时间点,则它应该在第一次使用与要初始化的对象在同一翻译单元中定义的任何函数或对象之前发生.

                  It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.

                  我没有任何其他保证(尤其是在不同翻译单元中定义的对象的初始化顺序是实现定义的).

                  Nothing else i guaranteed (especially order of initialization of objects defined in different translation units is implementation defined).

                  编辑正如 Suma 的评论中所指出的,还可以保证在输入 main 之前对它们进行初始化.

                  EDIT As pointed in Suma's comment, it is also guaranteed that they are initialized before main is entered.

                  这篇关于什么时候初始化静态 C++ 类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在类定义中定义静态常量整数成员 下一篇:C++ 静态虚拟成员?

                  相关文章

                • <tfoot id='8Gb31'></tfoot>

                  <small id='8Gb31'></small><noframes id='8Gb31'>

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

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