简体   繁体   English

static object 如何在编译时初始化?

[英]How static object gets initialized at compile time?

class Bird {
public:
   Bird() {
     .....
     .....
    }    
};

void fun() {
    static Bird obj;
}

When compiler compiles the statement static Bird obj It does 2 thing.当编译器编译语句static Bird obj它做了两件事。 First is memory allocation for object obj.首先是 memory 分配给 object obj。 Second is initialization of obj by calling constructor.其次是调用构造函数初始化obj。 My question is if initialization part happens in compile time, how all the statement inside constructor will be executed at compile time我的问题是如果初始化部分发生在编译时,构造函数中的所有语句将如何在编译时执行

When compiler compiles the statement static Bird obj It does 2 thing.当编译器编译语句 static Bird obj 它做了两件事。 First is memory allocation for object obj.首先是 memory 分配给 object obj。 Second is initialization of obj by calling constructor.其次是调用构造函数初始化obj。

No. The memory is already allocated at compile time (before the program gets executed).不。 memory 已经在编译时分配(在程序执行之前)。 It's just that initialization happens when the execution touches the static Bird obj;只是当执行触及static Bird obj; statement.陈述。 This is called lazy initialization .这称为延迟初始化

Also, note that, in case if Bird() constructor throws an exception, then the initialization will not be finished.另外,请注意,如果Bird()构造函数抛出异常,则初始化将不会完成。 So again when the fun() is called, obj is again tries to get initialized.所以再次调用fun()时, obj再次尝试初始化。 It happens until the obj initializes successfully.它会一直发生,直到obj成功初始化。 After that, that line will not be executed any more.之后,该行将不再执行。

At compile time, the compiler will set aside a chunk of memory in a special static object area which is part of the program space.在编译时,编译器将在一个特殊的 static object 区域中留出一块 memory,该区域是程序空间的一部分。 That memory will be uninitialized. memory 将未初始化。

Inside the function, the compiler will put an invisible "if" statement which detects that the static object statement is being executed for the first time.在 function 内部,编译器将放置一个不可见的“if”语句,该语句检测到 static object 语句正在第一次执行。 If it is the first time, the constructor for the object will be called.如果是第一次,将调用 object 的构造函数。

static initialization does not happen at compile time. static 初始化不会在编译时发生。 It happens at run-time, but before main() is invoked.它发生在运行时,但在调用 main() 之前。

The order in which static initialization spread across compilation units is not defined. static 初始化跨编译单元分布的顺序未定义。 Hence, if you really need static variables, the recommended way is to put all of them on a single static_constructors.cpp, and as a extra benefit, they'll be easier to find因此,如果你真的需要 static 变量,推荐的方法是将它们全部放在一个 static_constructors.cpp 中,作为额外的好处,它们会更容易找到

In that situation, static has a different meaning.在那种情况下, static有不同的含义。 It means that obj will be initialized only once, at the first time fun() is called and obj will remaing valid between calls to fun() .这意味着obj只会被初始化一次,第一次fun()被调用并且obj将在fun()调用之间保持有效。

Think of it as a global variable, but only the function fun() can see it:P将其视为全局变量,但只有 function fun()可以看到它:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM