C++ 中静态数据何时以及如何初始化?

发布于 2024-12-12 02:39:18 字数 268 浏览 0 评论 0原文

静态数据什么时候初始化?

我认为:

它可以通过以下方式初始化 构造函数,

或者

当它被声明时,

或者 课外被

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀞厅☆埖开 2024-12-19 02:39:18

类的静态成员在定义时初始化。常量整型数据类型是一个例外,可以在声明时进行初始化。何时执行此类初始化有点复杂(谷歌搜索静态初始化失败)。根据标准:

如果初始化被推迟到 main 的第一个语句之后的某个时间点,则它应在与要初始化的变量相同的翻译单元中定义的任何函数或变量的第一次 odr 使用之前发生。

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:

If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use of any function or variable defined in the same translation unit as the variable to be initialized.

断肠人 2024-12-19 02:39:18

静态存储持续时间对象初始化:

注意:静态成员对象的初始化方式与文件范围内的对象相同。

  • 文件范围内的对象在定义时初始化。
    • 您可以将它们视为在调用 main() 之前全部初始化。
    • 请参阅下面的详细信息。
  • 函数作用域中的对象在第一次执行传递到定义时被初始化。
    • ie 通常是第一次调用该函数。

该对象由定义中的初始值设定项初始化。如果您不提供初始化程序,那么它将被零初始化。

int x1;        // zero initialized.
int x2 = 5;    // initialized with 5 
A   y1;        // Default initialized:
               // If A has a user defined constructor this is called.
               // If A has a compiler generated constructor then
               // it is value-initialized which means class objects have their
               // default constructor called and POD objects are zero-initialized
A   y2 = A();  // Default constructed.
A   y3(5);     // Constructor called with value 5.

静态成员与文件范围内的对象完全相同。

class Z
{
    static int x1; // declaration;
    static int x2;
    static A   y1;
    static A   y2;
    static A   y3;
};
// definition

int Z::x1;
int Z::x2 = 5;
A   Z::y1;
A   Z::y2 = A();
A   Z::y3(5);

现在它们初始化的顺序更难定义。该命令故意保持模糊,以允许委员会无法预见的编译器和链接器情况。

它的定义在:

3.6.2非局部变量的初始化

主要需要注意的是:

具有静态存储持续时间的非局部变量在程序启动时被初始化。

所以在大多数情况下,它们都会在进入 main 之前完全构建完毕。

正如其他人指出的那样。允许编译器延迟初始化。

3.6.2 第 4 段

具有静态存储持续时间的非局部变量的动态初始化是否在 main 的第一条语句之前完成是实现定义的。

此添加主要是为了支持在运行时动态加载库(可以在 main 启动后动态加载)。但它确实提供了一个简单的保证,即在使用该编译中的任何对象或函数之前,将完全构造编译中的所有静态存储持续时间对象。

3.6.2 第 5 段

如果初始化被推迟到线程初始函数的第一个语句之后的某个时间点,则它应在同一翻译单元中定义的具有线程存储持续时间的任何变量的第一个 odr-use (3.2) 之前发生作为要初始化的变量。

Static Storage duration object initialization:

Note: Static member objects are initialized the same way as objects at file scope.

  • Objects at file scope are initialized at point of definition.
    • You can think of these as all being initialized before main() is called.
    • See details below.
  • Objects in function scope are initialized the first time execution passes over the definition.
    • ie Usually the first time the function is called.

The object is initialized by the initializer in the definition. if you do not provide an initializer then it will be zero-initialized.

int x1;        // zero initialized.
int x2 = 5;    // initialized with 5 
A   y1;        // Default initialized:
               // If A has a user defined constructor this is called.
               // If A has a compiler generated constructor then
               // it is value-initialized which means class objects have their
               // default constructor called and POD objects are zero-initialized
A   y2 = A();  // Default constructed.
A   y3(5);     // Constructor called with value 5.

Static members are exactly the same as objects as file scope.

class Z
{
    static int x1; // declaration;
    static int x2;
    static A   y1;
    static A   y2;
    static A   y3;
};
// definition

int Z::x1;
int Z::x2 = 5;
A   Z::y1;
A   Z::y2 = A();
A   Z::y3(5);

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:

Non-local variables with static storage duration are initialized as a consequence of program initiation.

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

It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main.

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

If the initialization is deferred to some point in time after the first statement of the initial function of the thread, it shall occur before the first odr-use (3.2) of any variable with thread storage duration defined in the same translation unit as the variable to be initialized.

稀香 2024-12-19 02:39:18

全局变量在程序启动时、调用 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.)

稀香 2024-12-19 02:39:18

不,当然,构造函数不能初始化静态数据成员。对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文