C ++: error C2440: '<function-style-cast>': cannot be converted from 'A <TYPE>' to 'B <TYPE>'

I have a base class A and it has a subclass of B. A overrides the + operator, and B also overrides it, calls the parent + operator and returns the result of B. Then I get an error message:

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

I thought polymorphism worked in such a way that it worked?

+1
source share
1 answer

In polymorphism, you cannot convert A to B , you can convert B to A B is a species of A, but A is not a species of B.

For example, in the classic Shape classes. If you have a Shape class and a Rectangle class that extends [inherit from] Shape , you cannot convert an instance of a Shape to a Rectangle , but you can use a Rectangle to Shape , because it is the "appearance" of the shape.

+8
source

All Articles