Forum > Windows

Function return parameter code generation misery

<< < (2/2)

Then you need to use advanced records with operator overloads. Anything else is just hacks.


--- Quote from: cryborg2 on November 06, 2022, 03:14:28 am ---I want to use the ":=" operator to get my virtual string type return value, and to do so, the called function must have acces to my VirtualString object -which have it's own memory management- to transfer data through function return mechanism.

--- End quote ---
You can, but not Pointer. Example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$LONGSTRINGS ON}{$MODESWITCH ADVANCEDRECORDS}{$APPTYPE CONSOLE}type  TVirtualString = record  strict private    FLength: SizeInt;    FData: PAnsiChar;    procedure CopyFrom(const Src; ALength: SizeInt);    class operator Copy(constref Src: TVirtualString; var Dst: TVirtualString);    class operator Finalize(var Self: TVirtualString);    class operator Initialize(var Self: TVirtualString);  public    class operator := (const S: string): TVirtualString;    function AsString: string;  end; class operator TVirtualString. := (const S: string): TVirtualString;begin  Result.CopyFrom(Pointer(S)^, Length(S));end; function TVirtualString.AsString: string;begin  SetString(Result, FData, FLength);end; class operator TVirtualString.Copy(constref Src: TVirtualString; var Dst: TVirtualString);begin  Dst.CopyFrom(Src.FData^, Src.FLength);end; class operator TVirtualString.Finalize(var Self: TVirtualString);begin  ReAllocMem(Self.FData, 0);  Self.FLength := 0;end; class operator TVirtualString.Initialize(var Self: TVirtualString);begin  Self.FLength := 0;  Self.FData := nil;end; procedure TVirtualString.CopyFrom(const Src; ALength: SizeInt);begin  FLength := ALength;  ReAllocMem(FData, FLength);  if FLength > 0 then    Move(Src, FData^, FLength)end; // End TVirtualString function SomeFunction: TVirtualString;begin  Result := '123';end; var  V: TVirtualString;begin  V := '456';  V := SomeFunction; // As you want  Writeln(V.AsString);  Readln;end.


[0] Message Index

[*] Previous page

Go to full version