[英]c++:error C2440: '<function-style-cast>' : cannot convert from 'A<TYPE>' to 'B<TYPE>'
I have a base class A and it has a subclass B. A overrides the + operator and B overrides it as well,calls the parent's +operator, and casts the result to B. Then I am getting the error message: 我有一个基类A,它有一个子类B。A覆盖了+运算符,B也覆盖了它,调用父级的+运算符,并将结果转换为B。然后,我得到了错误消息:
error C2440: '' : cannot convert from 'A' to 'B'
错误C2440:'':无法从'A'转换为'B'
I thought polymorphism worked in a way that should allow this to work? 我认为多态性应该以某种方式起作用?
In polymorphism you cannot convert A
to B
, you can convert B
to A
. 在多态中,不能将
A
转换为B
,可以将B
转换为A
B is a kind of A, but A is NOT a kind of B. B是A的一种,但A不是B的一种。
For example, in the classic Shape classes. 例如,在经典的Shape类中。 If you have a class
Shape
and a class Rectangle
that extends [inherit from] Shape
, you cannot convert a Shape
instance to Rectangle
, but you CAN cast a Rectangle
to Shape
, because it is a 'kind of' Shape. 如果您有一个
Shape
类和一个从Shape
继承而来的Rectangle
类,则不能将Shape
实例转换为Rectangle
,但是可以将Rectangle
为Shape
,因为它是Shape
的“一种”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.