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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8469900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:38:06  来源:igfitidea点击:

Can't downcast because class is not polymorphic?

c++inheritancepolymorphismvtable

提问by wfbarksdale

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

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

Example:

例子:

Class A(){
    int a;
    int getA(){return a;};
}


Class B(): A(){
    int b;
    int getB(){return b;};
}

In another class we are trying to downcast from an Aobject to a Bobject:

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

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

but this gives the following error:

但这给出了以下错误:

 cannot dynamic_cast ... (source type is polymorphic)

回答by Dave S

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

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

The reason why: static_castbasically 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.

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

dynamic_castcan 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.

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

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.

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

回答by tenorsax

You need at least one virtual method in a class for run-time type information (RTTI)to successfully apply dynamic_cast operator.

类中至少需要一个虚拟方法run-time type information (RTTI)才能成功应用 dynamic_cast 运算符。

回答by user993954

just make A destructor virtual (always do for any class just for safety).

只需将析构函数设为虚拟(为了安全起见,对任何类都这样做)。

回答by Murali Krishna

yes, dynamic_cast for non-polymorphic types are not allowed. The base class shall have at least one virtual method. Only then that class can be called as polymorphic.

是的,不允许用于非多态类型的 dynamic_cast。基类应至少有一个虚方法。只有这样,该类才能被称为多态。

This article explains a similar example: http://www.cplusplus.com/doc/tutorial/typecasting/

这篇文章解释了一个类似的例子:http: //www.cplusplus.com/doc/tutorial/typecasting/

回答by tyger

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

Here a is an object and b is a pointer.

这里 a 是一个对象, b 是一个指针。

Actually, upcasting and downcasting are both allowed in C++. But when using downcasting, 2 things should be pay attention to: 1 The superclass should has at least one virtual method. 2 Since superclass is "smaller" than subclass, one should use memory object carefully.

实际上,C++ 中允许向上转换和向下转换。但是在使用向下转换时,需要注意两点: 1 超类应该至少有一个虚方法。2 由于超类比子类“小”,因此应谨慎使用内存对象。