什么时候和如何在C ++中初始化静态数据? [英] When and how are static data initialized in C++?

查看:137
本文介绍了什么时候和如何在C ++中初始化静态数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态数据何时被初始化?

When are static data initialized ?

我认为:

它可以由
构造函数初始化,

It can be initialized by constructor,

,当它被声明时,


class A::member_d = 5; //  member_d is static  

别人?

此外,何时执行文件范围静态变量初始化,以及何时执行函数范围静态变量initizliaed?

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.

感谢

推荐答案

类的静态成员定义点。 Const积分数据类型是可以在声明点初始化的异常。当这样的初始化将要执行有点复杂(google为静态初始化fiasco )。根据标准:

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:


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

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.

这篇关于什么时候和如何在C ++中初始化静态数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆