• <tfoot id='qR4AQ'></tfoot>
    • <bdo id='qR4AQ'></bdo><ul id='qR4AQ'></ul>

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

        <legend id='qR4AQ'><style id='qR4AQ'><dir id='qR4AQ'><q id='qR4AQ'></q></dir></style></legend>

      2. 静态字段是否继承?

        时间:2023-08-02
        <i id='ZywQo'><tr id='ZywQo'><dt id='ZywQo'><q id='ZywQo'><span id='ZywQo'><b id='ZywQo'><form id='ZywQo'><ins id='ZywQo'></ins><ul id='ZywQo'></ul><sub id='ZywQo'></sub></form><legend id='ZywQo'></legend><bdo id='ZywQo'><pre id='ZywQo'><center id='ZywQo'></center></pre></bdo></b><th id='ZywQo'></th></span></q></dt></tr></i><div id='ZywQo'><tfoot id='ZywQo'></tfoot><dl id='ZywQo'><fieldset id='ZywQo'></fieldset></dl></div>
          <tbody id='ZywQo'></tbody>

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

        <tfoot id='ZywQo'></tfoot>

          • <bdo id='ZywQo'></bdo><ul id='ZywQo'></ul>

              • <legend id='ZywQo'><style id='ZywQo'><dir id='ZywQo'><q id='ZywQo'></q></dir></style></legend>
                • 本文介绍了静态字段是否继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当继承静态成员时,它们是整个层次结构的静态成员,还是该类的静态成员,即:

                  When static members are inherited, are they static for the entire hierarchy, or just that class, i.e.:

                  class SomeClass
                  {
                  public:
                      SomeClass(){total++;}
                      static int total;
                  };
                  
                  class SomeDerivedClass: public SomeClass
                  {
                  public:
                      SomeDerivedClass(){total++;}
                  };
                  
                  int main()
                  {
                      SomeClass A;
                      SomeClass B;
                      SomeDerivedClass C;
                      return 0;
                  }
                  

                  在所有三个实例中总共是 3,还是 SomeClass 是 2,SomeDerivedClass 是 1?

                  would total be 3 in all three instances, or would it be 2 for SomeClass and 1 for SomeDerivedClass?

                  推荐答案

                  3 在所有情况下,因为 SomeDerivedClass 继承的 static int total 正是 SomeDerivedClass 中的那个code>SomeClass,不是一个独特的变量.

                  3 in all cases, since the static int total inherited by SomeDerivedClass is exactly the one in SomeClass, not a distinct variable.

                  实际上 4 在所有情况下,正如@ejames 在他的回答中发现并指出的那样.

                  actually 4 in all cases, as @ejames spotted and pointed out in his answer, which see.

                  第二个问题中的代码在两种情况下都缺少 int,但添加它就可以了,即:

                  the code in the second question is missing the int in both cases, but adding it makes it OK, i.e.:

                  class A
                  {
                  public:
                      static int MaxHP;
                  };
                  int A::MaxHP = 23;
                  
                  class Cat: A
                  {
                  public:
                      static const int MaxHP = 100;
                  };
                  

                  工作正常,并且 A::MaxHP 和 Cat::MaxHP 的值不同——在这种情况下,子类不继承"基类的静态,因为,可以这么说,它隐藏"了它它自己的同名.

                  works fine and with different values for A::MaxHP and Cat::MaxHP -- in this case the subclass is "not inheriting" the static from the base class, since, so to speak, it's "hiding" it with its own homonymous one.

                  这篇关于静态字段是否继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我们在 C++ 中实际上需要 Private 或 Protect 下一篇:虚拟析构函数是继承的吗?

                  相关文章

                  最新文章

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

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

                    3. <legend id='bloBY'><style id='bloBY'><dir id='bloBY'><q id='bloBY'></q></dir></style></legend>

                      • <bdo id='bloBY'></bdo><ul id='bloBY'></ul>