Recent

Author Topic: Explicit initialization of class instances  (Read 1787 times)

Mario

  • New Member
  • *
  • Posts: 31
Explicit initialization of class instances
« on: July 01, 2024, 10:12:42 am »
Hi,

I'm trying to find out compact ways of initializing class instances, but I feel my gaps in the understanding of how this works are somewhat big because
my google-fu isn't helping me here.

Here a bit of code to showcase what I am trying to do: compactly initialize instances of object. What is the correct way of doing this?

Thanks!

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. program example;
  3.  
  4. uses fgl;
  5.  
  6. type
  7.    tfoo = class
  8.       x:  Integer;
  9.       y:  Double;
  10.    end;
  11.  
  12.    foolist = specialize TFPGList<tfoo>;
  13.  
  14. var
  15.    zz : tfoo =(x:5, y:3.14); // doesn't work
  16.    // and how about this?
  17.    // yy : foolist = ??
  18.  
  19. begin
  20. end.
  21.  

Thaddy

  • Hero Member
  • *****
  • Posts: 19269
  • Glad to be alive.
Re: Explicit initialization of class instances
« Reply #1 on: July 01, 2024, 10:48:09 am »
Classes can not be instantiated on declaration.
objects are fine constructs. You can even initialize them with constructors.

Zvoni

  • Hero Member
  • *****
  • Posts: 3399
Re: Explicit initialization of class instances
« Reply #2 on: July 01, 2024, 11:15:04 am »
Classes can not be instantiated on declaration.
And looking at his sample-code: Why classes and not records?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19269
  • Glad to be alive.
Re: Explicit initialization of class instances
« Reply #3 on: July 01, 2024, 11:35:02 am »
Records can be indeed delared like so:
Code: Pascal  [Select][+][-]
  1. var r:Record a,b:integer;end = (a:100;b:200);
  2. begin
  3.   writeln(r.a+r.b);
  4. end.
Records are not pointer types, but classes are. Classes can better be compared with pointers to records and these also need initialization.
That the above works is because the memory is fully allocated at declaration time.
« Last Edit: July 01, 2024, 12:32:41 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Mario

  • New Member
  • *
  • Posts: 31
Re: Explicit initialization of class instances
« Reply #4 on: July 01, 2024, 12:48:14 pm »
Classes can not be instantiated on declaration.
And looking at his sample-code: Why classes and not records?

Well, it's an attempt at a minimal example. I want to have a list of objects, and wonder if there is a better way than repeating

Code: Pascal  [Select][+][-]
  1. foolist.Add(tfoo.Create(...));

many times.

Thaddy

  • Hero Member
  • *****
  • Posts: 19269
  • Glad to be alive.
Re: Explicit initialization of class instances
« Reply #5 on: July 01, 2024, 02:19:16 pm »
Only if you implement the list as a record or use arrays instead of a list:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. // generates a two dimensional array with a total of 400 initialized elements
  3. type
  4.    TStaticArray<T; const lowbound,highbound: Integer> = array[lowbound..highbound] of T;
  5. var
  6.   // nested
  7.   bb:TStaticArray<TStaticArray<integer,200,299>,0,3>;
  8.   i:integer;
  9. begin
  10.   writeln(i, ' number of elements is: ',length(bb));
  11.   writeln(i, ' bounds are: ', low(bb):4,high(bb):4);
  12.   for i:= low(bb) to High(bb) do
  13.   begin
  14.     writeln(i, ' number of elements is: ',length(bb[i]));
  15.     writeln(i, ' bounds are: ', low(bb[i]):4,high(bb[i]):4);
  16.   end;
  17.   readln;  
  18. end.
Note that if you allocate such structures on the stack (meaning inside a procedure or function) you run the risk of a stackoverflow with larger numbers of items.
« Last Edit: July 01, 2024, 02:34:43 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

jamie

  • Hero Member
  • *****
  • Posts: 7773
Re: Explicit initialization of class instances
« Reply #6 on: July 01, 2024, 10:19:43 pm »
I just through this together for you to look at.

You can do much more than this, you can add operators to it, etc.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$ModeSwitch AdvancedRecords}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.   TmyXY = Record
  13.     x:Integer;
  14.     y:Double;
  15.   end;
  16.  
  17.  TFoo = Record
  18.   strict private
  19.    fXy:TMyXY;
  20.    Procedure SetValues(aX:Integer;aY:Double);
  21.    function GetFoo:TFoo;
  22.    Procedure SetFoo(aF:TFoo);
  23.   Public
  24.    constructor Foo(aX:Integer;aY:Double);
  25.    Property X:Integer read fxy.x write fxy.x;
  26.    Property Y:Double read fxy.y write fxy.y;
  27.    Property Me:TFoo read getFoo write setfoo;
  28.  end;
  29.  
  30.   { TForm1 }
  31.  
  32.   TForm1 = class(TForm)
  33.     Button1: TButton;
  34.     procedure Button1Click(Sender: TObject);
  35.   private
  36.  
  37.   public
  38.  
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45. {$R *.lfm}
  46. Procedure Tfoo.SetFoo(aF:Tfoo);
  47. Begin
  48.   fxy := af.fXy;
  49. end;
  50.  
  51. function Tfoo.GetFoo:TFoo;
  52. Begin
  53.   Result := Self;
  54. end;
  55.  
  56. Procedure Tfoo.SetValues(aX:Integer;AY:Double);
  57. Begin
  58.   fXY.x := aX; fXY.y :=aY;
  59. end;
  60.  
  61. constructor Tfoo.Foo(aX:Integer;aY:Double);
  62. Begin
  63.  fxy.x :=aX; fxy.y:=aY;
  64. end;
  65.  
  66. { TForm1 }
  67.  
  68. procedure TForm1.Button1Click(Sender: TObject);
  69. Var
  70.   F:Tfoo;
  71. begin
  72.   F.Foo(1,3.14);
  73.   Caption :=  F.y.Tostring;
  74. end;
  75.  
  76. end.
  77.  
  78.  
The only true wisdom is knowing you know nothing

Mario

  • New Member
  • *
  • Posts: 31
Re: Explicit initialization of class instances
« Reply #7 on: July 02, 2024, 10:37:04 am »
Thanks, I get it.

 

TinyPortal © 2005-2018