Recent

Author Topic: operator overloading ?  (Read 1882 times)

bluatigro

  • New Member
  • *
  • Posts: 26
  • everybody is diferent therefore everybody is equal
operator overloading ?
« on: February 19, 2019, 01:54:12 pm »
it is some time ago that i used lazarus
for a project

i want to create a single3d class whit operator overloading
for my openGL project's

how do i proceed ?
somthing like this ?
Code: [Select]
class TS3D
var
   x , y , z : single ;
end ;

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: operator overloading ?
« Reply #1 on: February 19, 2019, 02:12:14 pm »
Somethig like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TS3D = class
  3.     X, Y, Z: Single;
  4.   end;

Operator overloading – read this.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thaddy

  • Hero Member
  • *****
  • Posts: 14369
  • Sensorship about opinions does not belong here.
Re: operator overloading ?
« Reply #2 on: February 19, 2019, 03:37:14 pm »
A record is likely faster:
Code: Pascal  [Select][+][-]
  1. type
  2.   TS3D = record
  3.     X, Y, Z: Single;
  4.   end;
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: operator overloading ?
« Reply #3 on: February 19, 2019, 06:47:07 pm »
Steap ahead of you, unless you want to reinvent the wheel  ;)

https://github.com/Zaflis/nxpascal/blob/master/src/nxMath.pas#L395
The nxMath3D deals with 3D vectors and matrices.
« Last Edit: February 19, 2019, 06:48:57 pm by User137 »

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: operator overloading ?
« Reply #4 on: February 19, 2019, 11:14:40 pm »
A record is likely faster: […]

And less functional, the more if we need to have a collection in the form of a generic list. Because this is possible:

Code: Pascal  [Select][+][-]
  1. type
  2.   TS3D = class
  3.     X, Y, Z: Single;
  4.   end;
  5.  
  6. type
  7.   TS3DList = specialize TFPGObjectList<TS3D>;
  8.  
  9. var
  10.   List: TS3DList;
  11. begin
  12.   List := TS3DList.Create();
  13.   List.Add(TS3D.Create);
  14.  
  15.   List[0].X := 5.0;
  16.   {..}

but this not:

Code: Pascal  [Select][+][-]
  1. type
  2.   TS3D = record
  3.     X, Y, Z: Single;
  4.   end;
  5.  
  6. type
  7.   TS3DList = specialize TFPGList<TS3D>;
  8.  
  9. var
  10.   List: TS3DList;
  11. begin
  12.   List := TS3DList.Create();
  13.   List.Add(Default(TS3D));
  14.  
  15.   List[0].X := 5.0; // Error: Argument cannot be assigned to
  16.   {..}

It's okay if several records are needed, but with all their collections, either record or a generic list must be abandoned.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018