C++ 中静态数据何时以及如何初始化?
静态数据什么时候初始化?
我认为:
它可以通过以下方式初始化 构造函数,
或者
当它被声明时,
或者 课外被
class A::member_d = 5; // member_d is static
别人欺负?
另外,文件作用域静态变量何时初始化以及函数作用域静态变量何时初始化?
我认为它们在声明时就被初始化了。
谢谢
When are static data initialized ?
I think:
It can be initialized by
constructor,
or
when it is declared,
or
outside of the class by
class A::member_d = 5; // member_d is static
others ?
Also, When do file scope static variables initialized and when do function scope static variables initizliaed?
I think they are initialized when they are declared.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类的静态成员在定义时初始化。常量整型数据类型是一个例外,可以在声明时进行初始化。何时执行此类初始化有点复杂(谷歌搜索静态初始化失败)。根据标准:
Static members of a class are initialized at the point of definition. Const integral data types are an exception that can be initialized at the point of declaration. When such initialization is going to be executed is somewhat complicated (google for static initialization fiasco). According to the standard:
静态存储持续时间对象初始化:
注意:静态成员对象的初始化方式与文件范围内的对象相同。
该对象由定义中的初始值设定项初始化。如果您不提供初始化程序,那么它将被零初始化。
静态成员与文件范围内的对象完全相同。
现在它们初始化的顺序更难定义。该命令故意保持模糊,以允许委员会无法预见的编译器和链接器情况。
它的定义在:
3.6.2非局部变量的初始化
主要需要注意的是:
所以在大多数情况下,它们都会在进入 main 之前完全构建完毕。
正如其他人指出的那样。允许编译器延迟初始化。
3.6.2 第 4 段
此添加主要是为了支持在运行时动态加载库(可以在 main 启动后动态加载)。但它确实提供了一个简单的保证,即在使用该编译中的任何对象或函数之前,将完全构造编译中的所有静态存储持续时间对象。
3.6.2 第 5 段
Static Storage duration object initialization:
Note: Static member objects are initialized the same way as objects at file scope.
The object is initialized by the initializer in the definition. if you do not provide an initializer then it will be zero-initialized.
Static members are exactly the same as objects as file scope.
Now the order that they are initialized is harder to define. The order is deliberately left vague to allow for compiler and linker situations that the committee could not foresee.
It is defined in:
3.6.2 Initialization of non-local variables
The main thing to note is:
So in most situations they will all be fully constructed before main is entered.
As noted by others. The compiler is allowed to delay initialization.
3.6.2 Paragraph 4
This addition is mainly done to support dynamically loading libraries at runtime (which may by laded dynamically after main has started). But it does provide a simple guarantee that all static storage duration objects within a compilation until will be fully constructed before any objects or function in that compilation are used.
3.6.2 Paragraph 5
全局变量在程序启动时、调用
main()
之前初始化。作用域本地的静态对象在第一次执行经过它们时被初始化。静态类成员只是全局变量,所以见上文。
全局和静态对象的销毁发生在
main()
返回后。(其实现细节相当复杂,因为所有析构函数都必须在某处排队等待执行,并且对于本地静态,需要有一个标志来指示对象是否已被实例化。)
Global variables are initialized at program start-up, before
main()
is invoked. Static objects that are local to a scope are initialized the first time execution passes over them.Static class members are just global variables, so see above.
Destruction of global and static objects happens after
main()
returns.(The implementation details of this are fairly intricate, since all the destructors have to be queued up for execution somewhere, and for local statics there needs to be a flag to indicate whether the object has already been instantiated.)
不,当然,构造函数不能初始化静态数据成员。对于 const 整型或枚举类型,可以在类定义的范围内进行初始化。然而,一般来说,您必须在类定义之外进行初始化。
No, of course, the constructor can't initialize static data members. For const integral or enumeration types, you can initialize within the scope of the class definition. However, in general, you must initialize outside the class definition.