[Lazarus] object type: strange behaviour
Birger Jansen
birger at cnoc.nl
Fri Oct 22 11:44:27 CEST 2010
Forget my last message I upgraded FPC 2.4.1 to 2.5.1 and now this works like it should!
-----Oorspronkelijk bericht-----
Van: Birger Jansen <birger at cnoc.nl>
Verzonden: vr 22-10-2010 11:38
Aan: Lazarus at lists.lazarus.freepascal.org;
Onderwerp: [Lazarus] object type: strange behaviour
> Hi all,
>
> I'm converting a project from Delphi to FreePascal. It is a mathematical
> application that uses records with methods. Since this is not possible in FPC I
> am trying to convert this base type from record to object:
>
> TPoint = object
> public
> X,Y,Z: Double;
> class function Create(const Ax, Ay, Az: Double): TPoint;static;
> class function Distance(const P1, P2: TPoint): Double;static;
> end;
>
> class function TPoint.Create(const Ax, Ay, Az: Double): TPoint;static;
> begin
> Result.X := Ax;
> Result.Y := Ay;
> Result.Z := Az;
> end;
>
> class function TPoint.Distance(const P1, P2: TPoint): Double;static;
> var
> aX: Double;
> begin
> aX := Sqr(P1.X - P2.X) + Sqr(P1.Y - P2.Y) + Sqr(P1.Z - P2.Z);
> Result := Sqrt(aX);
> end;
>
> So far so good, this works (compiles and produces the desired result):
>
> var
> P1, P2: TPoint;
> d: Double;
> begin
> P1 := TPoint.Create(0,1,1);
> P2 := TPoint.Create(1,0,0);
> d := TPoint.Distance(P1, P2);
> WriteLn(d);
> ReadLn;
> end.
>
> Now there is a class type that has a TPoint field:
>
> TNode = class(TObject)
> public
> FCoord: TPoint; { coords }
> constructor Create(const X, Y, Z: Double);
> end;
>
> And a constructor that sets this point:
>
> constructor TNode.Create(const X, Y, Z: Double);
> begin
> FCoord.X := X;
> FCoord.Y := Y;
> FCoord.Z := Z;
> end;
>
> This works, however this doesn't:
>
> constructor TNode.Create(const X, Y, Z: Double);
> begin
> FCoord := TPoint.Create(X,Y,Z);
> end;
>
> It fails with an error: Class isn't a parent class of the current class. If I
> set TNode.FCoord outside the constructor there is no problem at all. Also, when
> I try to access other methods of TPoint this fails (see UpdateCoord in the full
> source below).
>
> Any idea what causes this behaviour? I included the complete code below for
> your reference.
>
> Kind regards,
> Birger Jansen
>
>
>
>
> program TestProject;
>
> {$mode delphi}{$H+}
>
> uses
> {$IFDEF UNIX}{$IFDEF UseCThreads}
> cthreads,
> {$ENDIF}{$ENDIF}
> Classes;
> type
>
> { TPoint }
>
> TPoint = object
> public
> X,Y,Z: Double;
> class function Create(const Ax, Ay, Az: Double): TPoint;
> class function Distance(const P1, P2: TPoint): Double;
> end;
>
> { TNode }
>
> TNode = class(TObject)
> public
> FCoord: TPoint; { coords }
> constructor Create(const X, Y, Z: Double);
> procedure UpdateCoord(const Value: TPoint);
> end;
>
> { TPoint }
>
> class function TPoint.Create(const Ax, Ay, Az: Double): TPoint;
> begin
> Result.X := Ax;
> Result.Y := Ay;
> Result.Z := Az;
> end;
>
> class function TPoint.Distance(const P1, P2: TPoint): Double;
> var
> aX: Double;
> begin
> aX := Sqr(P1.X - P2.X) + Sqr(P1.Y - P2.Y) + Sqr(P1.Z - P2.Z);
> Result := Sqrt(aX);
> end;
>
> constructor TNode.Create(const X, Y, Z: Double);
> begin
> FCoord.X := X;
> FCoord.Y := Y;
> FCoord.Z := Z;
> // this does not work:
> FCoord := TPoint.Create(X,Y,Z);
> end;
>
> procedure TNode.UpdateCoord(const Value: TPoint);
> begin
> // this does not work:
> if TPoint.Distance(FCoord, Value) < 1E-10 then
> FCoord := Value;
> end;
> end;
>
> var
> P1, P2: TPoint;
> N: TNode;
> d: Double;
>
> begin
> N := TNode.Create(1,2,3);
> // this works:
> N.FCoord := TPoint.Create(1,2,3);
> P1 := TPoint.Create(0,1,1);
> P2 := TPoint.Create(1,0,0);
> d := TPoint.Distance(P1, P2);
> WriteLn(d);
> ReadLn;
> end.
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>
More information about the Lazarus
mailing list