Recent

Author Topic: [Closed]Copy constructors being past as value to Function parameters.  (Read 251 times)

jamie

  • Hero Member
  • *****
  • Posts: 7832
Today I discovered that passing a record as value to a function will call the "Copy" Class Operator, in Delphi it's called the "Assign". This is good news, news I wish I knew earlier, which I believe did ask about but got a "I am not sure" answer, very understandable.

 I had thought I tested this before hand and concluded that it didn't call the COPY ( constructor ) when passing by value to a function but today, using 3.2.4, it seems I must of been incorrect or 3.2.4 has fixed this.

 I know it worked if doing a := between records.

Code: Pascal  [Select][+][-]
  1. TTestRec = Record
  2.   S :String;
  3.  Class Operator Copy(Constref Src:TTestRec;var Dest:TTestRec);
  4.  End;                                    
  5.  

But I see either I missed that or it now works with VALUE parameters in 3.2.4

Jamie




 
« Last Edit: July 07, 2026, 12:54:03 pm by jamie »
The only true wisdom is knowing you know nothing

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 456
  • I use FPC [main] 💪🐯💪
Re: Copy constructors being past as value to Function parameters.
« Reply #1 on: July 07, 2026, 05:22:01 am »
Code: Pascal  [Select][+][-]
  1. program app;
  2. {$mode objfpc}
  3. {$h+}
  4. {$modeswitch advancedrecords}
  5.  
  6. type
  7.   TTestRec = record
  8.     S: String;
  9.     class operator copy(constref src: TTestRec; var dest: TTestRec);
  10.   end;
  11.  
  12. class operator TTestRec.copy(constref src: TTestRec; var dest: TTestRec);
  13. begin
  14.   WriteLn('kek');
  15. end;
  16.  
  17. procedure kek1(A: TTestRec);
  18. begin
  19. end;
  20.  
  21. procedure kek2(const A: TTestRec);
  22. begin
  23. end;
  24.  
  25. procedure test;
  26. var
  27.   L: TTestRec;
  28. begin
  29.   kek1(L);
  30.   kek2(L);
  31. end;
  32.  
  33. begin
  34.   test;
  35.   ReadLn;
  36. end.

No "kek"

 :-X

(FPC git[main])
I may seem rude - please don't take it personally

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Copy constructors being past as value to Function parameters.
« Reply #2 on: July 07, 2026, 12:06:46 pm »
the "Const" is causing that because the compiler picks a REF instead of VALUE and that does not trigger the Copy.

  The same in C++, if its passed by REF

Jamie
The only true wisdom is knowing you know nothing

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 456
  • I use FPC [main] 💪🐯💪
Re: Copy constructors being past as value to Function parameters.
« Reply #3 on: July 07, 2026, 12:22:28 pm »
I won't understand what the problem is - or if there even is one - at that point
I may seem rude - please don't take it personally

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Copy constructors being past as value to Function parameters.
« Reply #4 on: July 07, 2026, 12:53:18 pm »
There does not appear to be any problem, compiler should adjust ref count for managed types in the record if past via value, it was the COPY operator that seems to be alluding me but appears to be working now as I would expect.

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018