<legend id='9mm5v'><style id='9mm5v'><dir id='9mm5v'><q id='9mm5v'></q></dir></style></legend>

    <small id='9mm5v'></small><noframes id='9mm5v'>

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

        <bdo id='9mm5v'></bdo><ul id='9mm5v'></ul>

      1. 在 C++ 中定义静态成员

        Defining static members in C++(在 C++ 中定义静态成员)
          <bdo id='J49N8'></bdo><ul id='J49N8'></ul>

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

                <legend id='J49N8'><style id='J49N8'><dir id='J49N8'><q id='J49N8'></q></dir></style></legend>
                  <tbody id='J49N8'></tbody>
              1. <i id='J49N8'><tr id='J49N8'><dt id='J49N8'><q id='J49N8'><span id='J49N8'><b id='J49N8'><form id='J49N8'><ins id='J49N8'></ins><ul id='J49N8'></ul><sub id='J49N8'></sub></form><legend id='J49N8'></legend><bdo id='J49N8'><pre id='J49N8'><center id='J49N8'></center></pre></bdo></b><th id='J49N8'></th></span></q></dt></tr></i><div id='J49N8'><tfoot id='J49N8'></tfoot><dl id='J49N8'><fieldset id='J49N8'></fieldset></dl></div>
                <tfoot id='J49N8'></tfoot>
                  本文介绍了在 C++ 中定义静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试像这样定义一个公共静态变量:

                  I am trying to define a public static variable like this :

                  public :
                           static int j=0;        //or any other value too
                  

                  我在这一行遇到编译错误:ISO C++ 禁止非常量静态成员 `j' 的类内初始化.

                  I am getting a compilation error on this very line : ISO C++ forbids in-class initialization of non-const static member `j'.

                  1. 为什么在 C++ 中不允许?

                  1. Why is it not allowed in C++ ?

                  为什么允许初始化 const 成员?

                  Why are const members allowed to be initialized ?

                  这是否意味着 C++ 中的静态变量不像 C 中那样用 0 初始化?

                  Does this mean static variables in C++ are not initialized with 0 as in C?

                  谢谢!

                  推荐答案

                  (1.) 为什么在 C++ 中不允许?

                  (1.) Why is it not allowed in C++ ?

                  来自 Bjarne Stroustrup 的 C++ 风格和技术常见问题解答:

                  类通常在头文件中声明,而头文件通常包含在许多翻译单元中.但是,为了避免复杂的链接器规则,C++ 要求每个对象都有唯一的定义.如果 C++ 允许在类中定义需要作为对象存储在内存中的实体,那么这条规则就会被打破.

                  A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.

                  (2.) 为什么允许 const 成员被初始化?

                  (2.) Why are const members allowed to be initialized ?

                  [直接说得更好]

                  (3.) 这是否意味着静态变量在 C++ 中没有用 0 初始化为在 C 中?

                  (3.) Does this mean static variables in C++ are not initialized with 0 as in C?

                  据我所知,只要您在 .cpp 中声明静态成员 var,如果您不另外指定,它将被零初始化:

                  As far as I know, as long as you declare the static member var in a .cpp it will be zero-initialized if you don't specify otherwise:

                  // in some .cpp
                  int Test::j; // j = int();
                  

                  这篇关于在 C++ 中定义静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                      <tfoot id='99vaa'></tfoot>
                        <legend id='99vaa'><style id='99vaa'><dir id='99vaa'><q id='99vaa'></q></dir></style></legend>

                        <small id='99vaa'></small><noframes id='99vaa'>

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

                              <tbody id='99vaa'></tbody>