简体   繁体   English

c ++:错误C2440:“ <function-style-cast> &#39;:无法从&#39;A转换 <TYPE> &#39;至&#39;B <TYPE> &#39;

[英]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 ,但是可以将RectangleShape ,因为它是Shape的“一种”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 C2440 '<function-style-cast> ': 无法从 'char' 转换为 'std::string'</function-style-cast> - Error C2440 '<function-style-cast>': cannot convert from 'char' to 'std::string' 错误C2440:“ <function-style-cast> &#39;:无法从“ LinkedList”转换 <int> :: Node *&#39;到&#39;LinkedListIterator <const T> &#39; - error C2440: '<function-style-cast>' : cannot convert from 'LinkedList<int>::Node *' to 'LinkedListIterator<const T>' 计时代码“C2440:”中的一个奇怪错误 <function-style-cast> &#39;:无法从&#39;_CR&#39;转换为&#39;std :: chrono :: milliseconds&#39;“ - A strange error in chrono code “C2440: '<function-style-cast>' : cannot convert from '_CR' to 'std::chrono::milliseconds'” 错误C2440:&#39;type cast&#39;:无法从&#39;overloaded-function&#39;转换为&#39;HOOKPROC&#39; - error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'HOOKPROC' 错误c2440&#39;=&#39;无法从int *转换为Type <T> * - Error c2440 '=' cannot convert from int * to Type<T> * C ++嵌套类模板错误C2440&#39;=&#39;:无法从&#39;type&#39;转换为&#39;same type&#39; - C++ Nested class template Error C2440 '=': cannot convert from 'type' to 'same type' 错误C2440:“类型转换”:无法从“布尔”转换为“ CString” - error C2440: 'type cast' : cannot convert from 'bool' to 'CString' 错误C2440:“类型转换”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“ DWORD” - error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'DWORD' C ++ C2440错误无法从&#39;const BWAPI :: UpgradeType&#39;转换为&#39;const BWAPI :: Type *&#39; - c++ C2440 error cannot convert from 'const BWAPI::UpgradeType' to 'const BWAPI::Type *' C ++类型转换:错误C2440:&#39;=&#39;:无法从&#39;short *&#39;转换为&#39;DCTBLOCK&#39; - C++ Type Casting: error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM