Covariant problem

Zarathustra adam.chrapkowski at gmail.com
Thu Jul 24 04:19:59 PDT 2008


Steven Schveighoffer Wrote:

> 
> "Zarathustra" wrote
> > Steven Schveighoffer Wrote:
> >
> >> "Zarathustra" wrote
> >> > error:
> >> > function basis.CPolygon2d.getPoints of type CPoint2d[]() overrides but 
> >> > is
> >> > not covariant with basis.CPolygon3d.getPoints of type CPoint3d[]()
> >> >
> >> > I don't know how to fix this problem. What does 'covariant' mean?
> >>
> >> Covariant means that the override returns something that can be 
> >> implicitly
> >> casted to the base version.
> >>
> >> So the error looks like the base class function getPoints returns type
> >> CPoint3d[], but the derived class is returning CPoint2d[].  In order for
> >> this to work, CPoint2d[] must be implicitly castable to CPoint3d[], which
> >> probably isn't true.
> >>
> >> In order to diagnose the problem exactly, we would have to see code.  At
> >> least the class hierarchy.
> >>
> >> -Steve
> >>
> >>
> >
> > Ok, thanks. I understand it now.
> > CPoint2d class is inherited by CPoint3d. Both classes have same named 
> > functions with this difference that one return CPoint2d object and second 
> > CPoin3d object. Is possible to extort to call correct function (not from 
> > parent class). It's strange, because CPoint2d can be casted to CPoint3d 
> > but not inversely.
> 
> I'm not quite sure how to interpret your statement.  It might be better to 
> understand by showing some stub code.
> 
> Fill in where you see ???:
> 
> class CPoint2d : ???
> {
> }
> 
> class CPoint3d : ???
> {
> }
> 
> class CPolygon2d : ???
> {
>   ???[] getPoints() {}
> }
> 
> class CPolygon3d : ???
> {
>   ???[] getPoints() {}
> }
> 
> -Steve 
> 
> 

class CPoint3d{
}

class CPoint2d : CPoint3d{
}

class CPolygon3d{
  CPoint3d [] getPoints() {}
}
class CPolygon2d : CPolygon3d{
  CPoint2d [] getPoints() {}
}


More information about the Digitalmars-d-learn mailing list