c ++:error C2440:'< function

编程入门 行业动态 更新时间:2024-10-25 12:20:40
本文介绍了c ++:error C2440:'< function-style-cast>' :不能从'A< TYPE>'到'B< TYPE>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个基类A,它有一个子类B. 覆盖+操作符,B也覆盖它,调用父类的+操作符,并将结果转换为B.然后我得到错误讯息:

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:

错误C2440:'':无法将'A'转换为'B'

error C2440: '' : cannot convert from 'A' to 'B'

我认为多态性的工作方式应该允许这个工作?

I thought polymorphism worked in a way that should allow this to work?

推荐答案

在多态性中,您不能将 A 转换为 B ,可以转换 B 到 A 。 B是一种A,但A不是B的一种。

In polymorphism you cannot convert A to B, you can convert B to A. B is a kind of A, but A is NOT a kind of B.

例如,在经典的Shape类中。如果你有一个类 Shape 和一个类 Rectangle extends [inherit from] Shape ,您不能将 Shape 实例转换为 Rectangle ,但是您可以转换 Rectangle 到 Shape ,因为它是一种形状。

For example, in the classic Shape classes. 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.

更多推荐

c ++:error C2440:'< function

本文发布于:2023-11-09 06:09:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:error   function   lt   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!