Covariant problem

Zarathustra adam.chrapkowski at gmail.com
Thu Jul 24 05:50:31 PDT 2008


Koroskin Denis Wrote:

> On Thu, 24 Jul 2008 15:19:59 +0400, Zarathustra  
> <adam.chrapkowski at gmail.com> wrote:
> 
> > 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() {}
> > }
> 
> Well, it *should* work based on current semantics, because CPoint2d[]  
> implicitly castable to CPoint3d[]. Moreover, it compiles with D2 but  
> doesn't with D1. However, it is error-prone (as I showed previously) and  
> is therefore better to avoid. Besides, it's an arguable design to inherit  
> CPoint2d from CPoint3d :)

Thanks, but Inheriting is ok, because point2d is also point3d but with this difference it is located on a plane.
Point2d have coordinates x, y, z in relation to the space coordinate system and coordinates x, y in relation to the plane coordinate system. But it's only thad by the way ;p
I use D1. Hmm in my opinion it should work. Ok little more code:
class CPoint3d{
  //______________________________________
  //  ::>> Attributes <<::
  //

  //______________________________________
  //  << fields >>

  private long m_x;
  private long m_y;
  private long m_z;
  //...

  //______________________________________
  //  << properties >>

  // property x
  //______________________________________
  public long x(long o_x){ return this.m_x = o_x; }
  public long x(            ){ return this.m_x          ; }

  // property y
  //______________________________________
  public long y(long o_y){ return this.m_y = o_y; }
  public long y(            ){ return this.m_y         ; }

  // property z
  //______________________________________
  public long z(long o_z){ return this.m_z = o_z; }
  public long z(            ){ return this.m_z         ; }

  //...
}
class CPoint2d : CPoint3d{
  //______________________________________
  //  ::>> Attributes <<::
  //

  //______________________________________
  //  << fields >>

  private long m_x;
  private long m_y;
  //...

  //______________________________________
  //  << properties >>

  // property x
  //______________________________________
  public long x(long o_x){ return this.m_x = o_x; }
  public long x(            ){ return this.m_x          ; }

  // property y
  //______________________________________
  public long y(long o_y){ return this.m_y = o_y; }
  public long y(            ){ return this.m_y         ; }

  //...
}

class CPolygon3d{
  //______________________________________
  //  ::>> Attributes <<::
  //

  //______________________________________
  //  << fields >>
  //...
  private CPoint3d [] m_points;
  //...

  //______________________________________
  //  << properties >>
  //...

  // property getPoints
  //______________________________________
  public CPoint3d [] getPoints(){ return this.GetPoints; }

  //...

  //______________________________________
  //  ::>> Methods <<::
  //
  
  //...

  //______________________________________
  //  << operations >>

  //...
  private CPoint3d []
  GetPoints()
  in{
    //...
  }
  out{
    //...
  }
  body{
    //...
  }
}

class CPolygon2d : CPolygon3d{
  //______________________________________
  //  ::>> Attributes <<::
  //

  //______________________________________
  //  << fields >>
  //...
  private CPoint2d [] m_points;
  //...

  //______________________________________
  //  << properties >>
  //...

  // property getPoints
  //______________________________________
  public CPoint2d [] getPoints(){ return this.GetPoints; } // <- error is here

  //...

  //______________________________________
  //  ::>> Methods <<::
  //
  
  //...

  //______________________________________
  //  << operations >>

  //...
  private CPoint2d []
  GetPoints()
  in{
    //...
  }
  out{
    //...
  }
  body{
    //...
  }
}

For me it's unreasonable because CPoint3d can not be casted to CPoint2d :(
Ech ;/


More information about the Digitalmars-d-learn mailing list