Recent

Author Topic: Record "inheritance"/structuring  (Read 303 times)

quantumfoam

  • New Member
  • *
  • Posts: 23
Record "inheritance"/structuring
« on: August 21, 2022, 06:09:13 pm »
Is it possible to somehow write composite records to simplify record definition like this?

Code: Pascal  [Select][+][-]
  1. type rec1 = record
  2.   a:integer; // offset 0, length 4 bytes
  3. end;
  4.  
  5. type rec2 = record(rec1) // does not compile
  6.   b:integer; // offset 4, length 4 bytes
  7. end;
  8.  
  9. var x:rec2;
  10.  
  11. begin
  12.   x.a := 1;
  13.   x.b := 2;
  14. end.
  15.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 5563
Re: Record "inheritance"/structuring
« Reply #1 on: August 21, 2022, 06:26:31 pm »
Use a class.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

quantumfoam

  • New Member
  • *
  • Posts: 23
Re: Record "inheritance"/structuring
« Reply #2 on: August 21, 2022, 06:34:56 pm »
@MarkMLl. This would be too slow for my purposes, since each object needs a create call. I need to allocate many of such records into pages, and access them via pointers.
EDIT: Also, I don't need any methods/functions attached to these records. Just plain data.
« Last Edit: August 21, 2022, 06:37:31 pm by quantumfoam »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Record "inheritance"/structuring
« Reply #3 on: August 21, 2022, 06:38:45 pm »
@MarkMLl. This would be too slow for my purposes, since each object needs a create call. I need to allocate many of such records into pages, and access them via pointers.
use objects then
a quick feature comparison
https://wiki.freepascal.org/Record#Records_compared_to_other_structured_types

quantumfoam

  • New Member
  • *
  • Posts: 23
Re: Record "inheritance"/structuring
« Reply #4 on: August 21, 2022, 07:42:57 pm »
@HeavyUser, thanks. I didn't know about Object, I only used Classes before. And I didn't know that class can be "created" via New keyword. Anyway, since Objects can be created in arrays without instantion/creation of every single instance (contrary to Classes), I'll use Objects.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type rec2 = record
  6.   a,b,c:integer;
  7. end;
  8. type prec2 = ^rec2;
  9.  
  10. type obj1 = object
  11.   a,b:integer;
  12. end;
  13.  
  14. type obj2 = object(obj1)
  15.   c:integer;
  16.   procedure add;
  17. end;
  18. type pobj2 = ^obj2;
  19.  
  20. type cls1 = class
  21.   a,b:integer;
  22. end;
  23. type cls2 = class(cls1)
  24.   c:integer;
  25.   procedure add;
  26. end;
  27. type pcls2 = ^cls2;
  28.  
  29.  
  30. procedure obj2.add;
  31. begin
  32.   c := c + 4;
  33. end;
  34.  
  35. procedure cls2.add;
  36. begin
  37.   c := c + 4;
  38. end;
  39.  
  40. var o:pobj2; c:pcls2;
  41.   page:array of obj2;
  42.   i:integer;
  43.   const cnt = 3;
  44.  
  45.  
  46.  
  47. begin
  48.   writeln(sizeof(rec2)); // 12
  49.   writeln(sizeof(obj2)); // 12
  50.   writeln(sizeof(cls2)); // 4
  51.   setlength(page, cnt);
  52.   for i := 0 to cnt - 1 do begin
  53.     page[i].c := i;
  54.     page[i].add;
  55.     writeln(page[i].c);
  56.   end;
  57.   //new(o);
  58.   //new(c);
  59. end.
  60.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 4744
  • Compiler Developer
Re: Record "inheritance"/structuring
« Reply #5 on: August 22, 2022, 09:15:00 am »
And I didn't know that class can be "created" via New keyword.

  • Classes can not be instantiated via New, they are only instantiated by using a constructor
  • New is not a keyword; it's an intrinsic and works on any typed pointer variable

 

TinyPortal © 2005-2018