• <small id='TT4Ln'></small><noframes id='TT4Ln'>

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

          <bdo id='TT4Ln'></bdo><ul id='TT4Ln'></ul>

        不能因为类不是多态而沮丧吗?

        时间:2023-08-02
      1. <tfoot id='Sh7P9'></tfoot>

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

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

                  <tbody id='Sh7P9'></tbody>
                <legend id='Sh7P9'><style id='Sh7P9'><dir id='Sh7P9'><q id='Sh7P9'></q></dir></style></legend>
                1. 本文介绍了不能因为类不是多态而沮丧吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以在没有虚方法的情况下进行继承?编译器说下面的代码不是多态的.

                  Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic.

                  示例:

                  class A {
                  public:
                      int a;
                      int getA(){return a;};
                  }
                  
                  
                  class B : public A {
                  public:
                      int b;
                      int getB(){return b;};
                  }
                  

                  在另一个类中,我们试图从 A 对象向下转换为 B 对象:

                  In another class we are trying to downcast from an A object to a B object:

                   A *a = ...;
                   B *b = dynamic_cast<B*>(a)
                  

                  但这会导致以下编译时错误:

                  but this gives the following compile-time error:

                   cannot dynamic_cast ... (source type is not polymorphic)
                  

                  推荐答案

                  语法错误,您不能dynamic_cast 非多态类型.static_cast 是您将在这种情况下使用的强制转换,如果您知道它实际上是目标类型的对象.

                  Syntax errors non-withstanding, you cannot dynamic_cast a non-polymorphic type. static_cast is the cast you would use in this case, if you know that it is in fact an object of the target type.

                  原因:static_cast 基本上是让编译器在编译时执行检查输入是否可以强制转换为输出?"这可用于向上或向下转换指针(或引用)的继承层次结构的情况.但检查仅在编译时进行,编译器假定您知道自己在做什么.

                  The reason why: static_cast basically has the compiler perform a check at compile time "Could the input be cast to the output?" This is can be used for cases where you are casting up or down an inheritance hierarchy of pointers (or references). But the check is only at compile time, and the compiler assumes you know what you are doing.

                  dynamic_cast 只能在指针或引用强制转换的情况下使用,并且除了编译时检查之外,它还会进行额外的运行时检查以确保强制转换是否合法.它要求有问题的类至少有 1 个虚方法,这允许编译器(如果它支持 RTTI)执行这个额外的检查.但是,如果所讨论的类型没有任何虚方法,则无法使用.

                  dynamic_cast can only be used in the case of a pointer or reference cast, and in addition to the compile time check, it does an additional run time check that the cast is legal. It requires that the class in question have at least 1 virtual method, which allows the compiler (if it supports RTTI) to perform this additional check. However, if the type in question does not have any virtual methods, then it cannot be used.

                  最简单的情况,如果你像这样传递指针可能是值得的,就是考虑使基类的析构函数成为虚拟的.除了允许您使用动态转换之外,它还允许在删除基类指针时调用适当的析构函数.

                  The simplest case, and probably worthwhile if you're passing pointers around like this, is to consider making the base class's destructor virtual. In addition to allowing you to use dynamic cast, it also allows the proper destructors to be called when a base class pointer is deleted.

                  这篇关于不能因为类不是多态而沮丧吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:所有的虚函数都需要在派生类中实现吗? 下一篇:C++11 虚拟析构函数和移动特殊函数的自动生成

                  相关文章

                  最新文章

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

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

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