Skip to content

Commit 64df852

Browse files
committed
Show class operator that actually works with Delphi
1 parent 025df47 commit 64df852

5 files changed

+693
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{$ifdef FPC}
2+
{$mode objfpc}{$H+}{$J-}
3+
{$modeswitch advancedrecords}
4+
{$endif}
5+
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
6+
7+
uses SysUtils;
8+
9+
type
10+
TVector3 = record
11+
public
12+
X, Y, Z: Single;
13+
class operator {$ifdef FPC}+{$else}Add{$endif}
14+
(const A, B: TVector3): TVector3;
15+
class operator {$ifdef FPC}*{$else}Multiply{$endif}
16+
(const V: TVector3; const Scalar: Single): TVector3;
17+
function ToString: String;
18+
end;
19+
20+
class operator TVector3.{$ifdef FPC}+{$else}Add{$endif}
21+
(const A, B: TVector3): TVector3;
22+
begin
23+
Result.X := A.X + B.X;
24+
Result.Y := A.Y + B.Y;
25+
Result.Z := A.Z + B.Z;
26+
end;
27+
28+
class operator TVector3.{$ifdef FPC}*{$else}Multiply{$endif}
29+
(const V: TVector3; const Scalar: Single): TVector3;
30+
begin
31+
Result.X := V.X * Scalar;
32+
Result.Y := V.Y * Scalar;
33+
Result.Z := V.Z * Scalar;
34+
end;
35+
36+
function TVector3.ToString: String;
37+
begin
38+
Result := Format('(%f, %f, %f)', [X, Y, Z]);
39+
end;
40+
41+
var
42+
V1, V2: TVector3;
43+
begin
44+
V1.X := 1.0; V1.Y := 2.0; V1.Z := 3.0;
45+
V2.X := 4.0; V2.Y := 5.0; V2.Z := 6.0;
46+
WriteLn('V1: ', V1.ToString);
47+
WriteLn('V2: ', V2.ToString);
48+
WriteLn('V1 + V2: ', (V1 + V2).ToString);
49+
WriteLn('V1 * 10: ', (V1 * 10).ToString);
50+
end.

0 commit comments

Comments
 (0)