Forum > Beginners

Record "inheritance"/structuring

(1/3) > >>

quantumfoam:
Is it possible to somehow write composite records to simplify record definition like this?


--- 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";}};} ---type rec1 = record  a:integer; // offset 0, length 4 bytesend; type rec2 = record(rec1) // does not compile  b:integer; // offset 4, length 4 bytesend; var x:rec2; begin  x.a := 1;  x.b := 2;end. 

MarkMLl:
Use a class.

MarkMLl

quantumfoam:
@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.

HeavyUser:

--- Quote from: quantumfoam 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.

--- End quote ---
use objects then
a quick feature comparison
https://wiki.freepascal.org/Record#Records_compared_to_other_structured_types

quantumfoam:
@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  [+][-]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";}};} ---program Project1; {$mode objfpc}{$H+} type rec2 = record  a,b,c:integer;end;type prec2 = ^rec2; type obj1 = object  a,b:integer;end; type obj2 = object(obj1)  c:integer;  procedure add;end;type pobj2 = ^obj2; type cls1 = class  a,b:integer;end;type cls2 = class(cls1)  c:integer;  procedure add;end;type pcls2 = ^cls2;  procedure obj2.add;begin  c := c + 4;end; procedure cls2.add;begin  c := c + 4;end; var o:pobj2; c:pcls2;  page:array of obj2;  i:integer;  const cnt = 3;   begin  writeln(sizeof(rec2)); // 12  writeln(sizeof(obj2)); // 12  writeln(sizeof(cls2)); // 4  setlength(page, cnt);  for i := 0 to cnt - 1 do begin    page[i].c := i;    page[i].add;    writeln(page[i].c);  end;  //new(o);  //new(c);end. 

Navigation

[0] Message Index

[#] Next page

Go to full version