Can#39;t downcast because class is not polymorphic?(不能因为类不是多态而沮丧?)
问题描述
是否可以在没有虚拟方法的情况下进行继承?编译器说下面的代码不是多态的.
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.
这篇关于不能因为类不是多态而沮丧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不能因为类不是多态而沮丧?
基础教程推荐
- C++ #define 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C++输入/输出运算符重载 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- C++定义类对象 1970-01-01
- C语言访问数组元素 1970-01-01
- C++按值调用 1970-01-01
- 初始化变量和赋值运算符 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01