Recent

Author Topic: Is there an easy way to initialize an array of objects in free pascal? [SOLVED]  (Read 1221 times)

YiannisKam

  • Jr. Member
  • **
  • Posts: 99
In C++ I can initialize an array of objects like this:
Code: C  [Select][+][-]
  1. Person persons[3] = { Person("John", 1.77, 80), Person("Maria", 1.65, 52), Person("Jim", 1.85, 95) };
Is there a similar way that can be done in free pascal?
« Last Edit: January 31, 2023, 05:46:45 pm by YiannisKam »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #1 on: January 30, 2023, 02:03:33 am »
Code: Pascal  [Select][+][-]
  1. Type
  2.   TPersons = Record
  3.               Name:String;
  4.               Num1,Num2:Single;
  5.              end;
  6. Const
  7.     Persons:array[0..1]of TPersons=((Name:'Crap';Num1:1.7;Num2:80),(Name:'Crapper';Num1:1.9;Num2:85));
  8.  

 Thats one way.

you can also do this within the VAR section too but you need a type;
The only true wisdom is knowing you know nothing

YiannisKam

  • Jr. Member
  • **
  • Posts: 99
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #2 on: January 30, 2023, 02:30:23 am »
Person is a class and it doesn't work. Here is the code I'm trying to make it work
Code: Pascal  [Select][+][-]
  1. program ArrayOfObjects;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type
  6.  
  7.   { TPerson }
  8.  
  9.   TPerson = class
  10.   public
  11.     name: string;
  12.     height: real;
  13.     weight: integer;
  14.  
  15.     constructor Create(p_name: string; p_height: real; p_weight: integer);
  16.     procedure printPerson;
  17.   private
  18.  
  19.   end;
  20.  
  21. { TPerson }
  22.  
  23. constructor TPerson.Create(p_name: string; p_height: real; p_weight: integer);
  24. begin
  25.   self.name := p_name;
  26.   self.height := p_height;
  27.   self.weight := p_weight;
  28. end;
  29.  
  30. procedure TPerson.printPerson;
  31. begin
  32.   writeln('Name: ', self.name);
  33.   writeln('Height: ', self.height:0:2);
  34.   writeln('Weight: ', self.weight);
  35.   writeln;
  36. end;
  37.  
  38. {
  39. this doesn't work
  40. const
  41.   persons: array[0..2] of TPerson = ((name:'John'; height:1.77; weight: 90),
  42.                                       (name:'Maria'; height:1.65; weight: 52),
  43.                                       (name:'Jim'; height:1.85; weight: 95));
  44. }
  45. var
  46.   i: Integer;
  47.  
  48.  
  49. begin
  50.  
  51.  
  52.   for i := Low(persons) to High(persons) do
  53.   begin
  54.     persons[i].printPerson;
  55.   end;
  56.  
  57.   readln;
  58. end.
  59.  

I know I can do:
Code: Pascal  [Select][+][-]
  1.   persons[0] := TPerson.Create('John', 1.77, 90);
  2.   persons[1] := TPerson.Create('Maria', 1.65, 52);
  3.   persons[2] := TPerson.Create('Jim', 1.85, 95);
  4.  
but out of curiosity, I'm looking something more compact if already exists

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #3 on: January 30, 2023, 04:25:30 am »
Person is a class and it doesn't work.

I know I can do:
Code: Pascal  [Select][+][-]
  1.   persons[0] := TPerson.Create('John', 1.77, 90);
  2.   persons[1] := TPerson.Create('Maria', 1.65, 52);
  3.   persons[2] := TPerson.Create('Jim', 1.85, 95);
  4.  
but out of curiosity, I'm looking something more compact if already exists

More compact, possibly. You can't assign a class instance to a const outside a parameter list.

You can assign a assign class instances to a const var parameter though. But you will have to free them though, as class instances are allocated off the heap, not the stack.

The following works:
Code: Pascal  [Select][+][-]
  1. program ArrayOfObjects;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type
  6.  
  7.   { TPerson }
  8.  
  9.   TPerson = class
  10.   public
  11.     name: string;
  12.     height: real;
  13.     weight: integer;
  14.  
  15.     constructor Create(p_name: string; p_height: real; p_weight: integer);
  16.     procedure printPerson;
  17.   private
  18.  
  19.   end;
  20.  
  21. { TPerson }
  22.  
  23. constructor TPerson.Create(p_name: string; p_height: real; p_weight: integer);
  24. begin
  25.   self.name := p_name;
  26.   self.height := p_height;
  27.   self.weight := p_weight;
  28. end;
  29.  
  30. procedure TPerson.printPerson;
  31. begin
  32.   writeln('Name: ', self.name);
  33.   writeln('Height: ', self.height:0:2);
  34.   writeln('Weight: ', self.weight);
  35.   writeln;
  36. end;
  37.  
  38. procedure main(const persons: array of TPerson);
  39.   var
  40.     i: Integer;
  41.   begin
  42.     for i := Low(persons) to High(persons) do
  43.     begin
  44.       persons[i].printPerson;
  45.     end;
  46.     for i := Low(persons) to High(persons) do
  47.     begin
  48.       persons[i].free;
  49.     end;
  50.    readln;
  51.   end;
  52.  
  53. begin
  54.   main([TPerson.Create('John', 1.77, 90), TPerson.Create('Maria', 1.65, 52), TPerson.Create('Jim', 1.85, 95)]);
  55. end.

YiannisKam

  • Jr. Member
  • **
  • Posts: 99
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #4 on: January 30, 2023, 05:09:24 am »
Thanks Bogen85, I didn't know that kind of syntax but to be honest it looks more counterintuitive to me. I think I'll go with this
Code: Pascal  [Select][+][-]
  1.   persons[0] := TPerson.Create('John', 1.77, 90);
  2.   persons[1] := TPerson.Create('Maria', 1.65, 52);
  3.   persons[2] := TPerson.Create('Jim', 1.85, 95);
  4.  
although it's cumbersome and ugly.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #5 on: January 30, 2023, 06:12:23 am »
FPC-3.3.1 supports constructors for static arrays. So it seems possible to write
Code: Pascal  [Select][+][-]
  1.   persons := [TPerson.Create('John', 1.77, 90), TPerson.Create('Maria', 1.65, 52), TPerson.Create('Jim', 1.85, 95)];
  2.  

or even
Code: Pascal  [Select][+][-]
  1. ...
  2. function Person(p_name: string; p_height: real; p_weight: integer): TPerson;
  3. begin
  4.   Result := TPerson.Create(p_name, p_height, p_weight);
  5. end;
  6. ...
  7.   persons := [Person("John", 1.77, 90), Person("Maria", 1.65, 52), Person("Jim", 1.85, 95)];
  8.  
  9.  

dje

  • Full Member
  • ***
  • Posts: 134
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #6 on: January 30, 2023, 07:21:01 am »
I could be wrong (I'm not a C++ expert), but since the C++ code isn't using the new operator, then the Pascal equivalent is object types or advanced records. Using a class in this situation will create a minefield of memory leaks, and you may then blame Pascal.

I would implement as such:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TPerson = object
  4.     Name: String;
  5.     Num1, Num2: Single;
  6.   end;
  7.  
  8. function Person(Name: String; Num1, Num2: Single): TPerson;
  9. begin
  10.   Result.Name := Name;
  11.   Result.Num1 := Num1;
  12.   Result.Num2 := Num2;
  13. end;
  14.  
  15. var Persons: array of TPerson;
  16.  
  17. begin
  18.   Persons := [Person('John', 1.77, 80), Person('Maria', 1.65, 52), Person('Jim', 1.85, 95)];  
  19. end.
  20.  

If you really need OOP then you should be using a storage method which owns the objects. ie: TComponent, TObectList, etc.

YiannisKam

  • Jr. Member
  • **
  • Posts: 99
Re: Is there an easy way to initialize an array of objects in free pascal?
« Reply #7 on: January 31, 2023, 05:45:58 pm »
Sorry for my delayed reply guys. Thank you all for the information. I've downloaded the FPC-3.3.1 trunk version.

 

TinyPortal © 2005-2018