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

        • <bdo id='cu37L'></bdo><ul id='cu37L'></ul>
      1. <legend id='cu37L'><style id='cu37L'><dir id='cu37L'><q id='cu37L'></q></dir></style></legend>

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

      2. 静态初始化顺序失败

        时间:2023-12-02

              <tbody id='7EYij'></tbody>
            <legend id='7EYij'><style id='7EYij'><dir id='7EYij'><q id='7EYij'></q></dir></style></legend>

            <small id='7EYij'></small><noframes id='7EYij'>

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

              • <bdo id='7EYij'></bdo><ul id='7EYij'></ul>

                  本文介绍了静态初始化顺序失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在他的Thinking in C++"(第 10 章)中,Eckel 描述了一种由 Jerry Schwarz 首创的技术来解决这场惨败.他说如果我们想将 x 初始化为 100 并将 y 初始化为 200 并在所有翻译单元之间共享它们,我们会创建一个如下所示的 Initializer.h:>

                  In his "Thinking in C++" (Chapter 10) Eckel describes a technique that was pioneered by Jerry Schwarz to solve the fiasco. He says that if we want to initialize x to 100 and y to 200 and share them among all translation units, we create an Initializer.h that looks like this:

                  extern int x;
                  extern int y;
                  class Initializer {
                     static int initCount;
                     // if (initCount++ == 0) x = 100 & y = 200
                     /* ... */
                  };
                  static Initializer init;
                  

                  在实现文件中我们有

                  #include "Initializer.h"
                  int x;
                  int y;
                  int Initializer::initCount;
                  

                  并且 Eckel 说静态初始化(在实现文件中)将强制所有这些值为零".

                  and Eckel says that "static initialization (in implementation file) will force all these values to zero".

                  让我考虑以下情况:编译器在包含该标头的其他文件之后处理实现文件(这意味着 x 和 y 已在该其他文件中设置为 100 和 200).编译器看到int x,它会怎么做?它是否会将 x 和 y 设置为零以消除初始化和先前文件中所有可能的更改?但如果是这样,那么 initCount 也将被设置为零,从而破坏整个技术.

                  Let me consider the following case: the compiler processes the implementation file after some other file with that header included (it means that x and y have been already set to 100 and 200 in that other file). The compiler sees int x, so what will it do? Will it set x and y to zero eliminating initialization and all possible changes in previous files? But if it does, then initCount will also be set to zero, breaking down the whole technique.

                  推荐答案

                  但如果它是真的,并且编译器在另一个文件之后处理实现文件,那么它会将 x 和 y 设置为零以消除初始化和先前文件中所有可能的更改?

                  But if it is true, and the compiler handles the implementation file after some another file, than it will set x and y to zero eliminating initialization and all possible changes in previous files?

                  我不确定你的意思.如果在其他文件中定义了 xy,那么您就会发生链接器冲突,并且程序根本无法编译.

                  I'm not sure what you mean by this. If x and y are defined in other files, then you have a linker clash and the program simply won't compile.

                  如果 xy 和最重要的 Initializer::initCount 以这种方式实现,它们将在程序;它们实际上是全局的,将在程序开始时初始化为 0之前 任何 Initializer 被构造(由于包含声明一个 static 该类的实例).static Initializer 的每个构造都会首先检查是否由于 if (initCount++ == 0) 等原因构造了任何其他 Initializer.

                  If x, y and most importantly Initializer::initCount are implemented in this way, there will be unique instances of them in the program; they are effectively global and will be initialized to 0 at program start, before any Initializer is constructed (due to inclusion of the header declaring a static instance of that class). Each construction of a static Initializer will first check whether any other Initializers have been constructed due to the if (initCount++ == 0) etc.

                  要运行的第一个 Initializer 构造函数(仍在进入 main 之前)将因此设置所有三个值.

                  The first Initializer ctor to run (still before entering main) will thus set all three values.

                  这篇关于静态初始化顺序失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“未定义的引用"到从静态方法访问的模板类的静态成员 下一篇:可以在 C++ 中将类声明为静态吗?

                  相关文章

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

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

                      • <bdo id='GF3Tr'></bdo><ul id='GF3Tr'></ul>
                    2. <tfoot id='GF3Tr'></tfoot>
                      <legend id='GF3Tr'><style id='GF3Tr'><dir id='GF3Tr'><q id='GF3Tr'></q></dir></style></legend>