Идея такова, хранить в одной записи, разные записи через вариантную запись в pascal. Вот код.
- Код: Выделить всё
{$mode objfpc}{$H+}
program main;
type
Attr = (Button, CheckBox);
type
PButton = ^TButton;
TButton = record
Id : integer;
PosX : integer;
PosY : integer;
SizeX : integer;
SizeY : integer;
Name : string;
end;
type
PCheckBox = ^TCheckBox;
TCheckBox = record
Id : integer;
PosX : integer;
PosY : integer;
SizeX : integer;
SizeY : integer;
Stat : boolean;
Name : string;
end;
type
PWidget = ^TWidget;
TWidget = record
Next: PWidget;
case Tag: Attr of
Button : (Button :TButton);
CheckBox: (CheckBox :TCheckBox);
end;
var
Table: PWidget;
Ids: integer;
i : PWidget;
function ButtonCreate(Name: string): integer;
var
P: PWidget;
begin
New(P);
P^.Tag := Button;
Inc(Ids);
P^.Button.Id := Ids;
P^.Button.Name := Name;
P^.Next := Table;
Table := P;
ButtonCreate := P^.Button.Id;
end;
function CheckBoxCreate(Name: string): integer;
var
P: PWidget;
begin
New(P);
P^.Tag := CheckBox;
Inc(Ids);
P^.CheckBox.Id := Ids;
P^.CheckBox.Name := Name;
P^.Next := Table;
Table := P;
CheckBoxCreate := P^.CheckBox.Id;
end;
begin
Ids := -1;
ButtonCreate('1');
CheckBoxCreate('2');
i := table;
while (i <> NIL) do
begin
if (i^.Tag = Button) then
begin
writeLn(i^.Button.Name);
end;
if (i^.Tag = CheckBox) then
begin
writeLn(i^.CheckBox.Name);
end;
i := I^.Next;
end;
end.
Всё работает за исключение динамических строк. Если вписать директиву {$H+}
Компилятор выдаёт ошибку
Error: Data types which require initialization/finalization can'
t be used in variant records
Не хотелось бы отказываться от строк длиннее 255.
Добавлено спустя 58 минут 19 секунд:
Заменил string на PString
- Код: Выделить всё
{$mode objfpc}{$H+}
program main;
type
Attr = (Button, CheckBox);
type
PButton = ^TButton;
TButton = record
Id : integer;
PosX : integer;
PosY : integer;
SizeX : integer;
SizeY : integer;
Name : pstring;
end;
type
PCheckBox = ^TCheckBox;
TCheckBox = record
Id : integer;
PosX : integer;
PosY : integer;
SizeX : integer;
SizeY : integer;
Stat : boolean;
Name : pstring;
end;
type
PWidget = ^TWidget;
TWidget = record
Next: PWidget;
case Tag: Attr of
Button : (Button :TButton);
CheckBox: (CheckBox :TCheckBox);
end;
var
Table: PWidget;
Ids: integer;
i : PWidget;
function ButtonCreate(Name: string): integer;
var
P: PWidget;
begin
New(P);
P^.Tag := Button;
Inc(Ids);
P^.Button.Id := Ids;
P^.Button.Name := Pstring(Name);
P^.Next := Table;
Table := P;
ButtonCreate := P^.Button.Id;
end;
function CheckBoxCreate(Name: string): integer;
var
P: PWidget;
begin
New(P);
P^.Tag := CheckBox;
Inc(Ids);
P^.CheckBox.Id := Ids;
P^.CheckBox.Name := Pstring(Name);
P^.Next := Table;
Table := P;
CheckBoxCreate := P^.CheckBox.Id;
end;
begin
Ids := -1;
ButtonCreate('1');
CheckBoxCreate('2');
i := table;
while (i <> NIL) do
begin
if (i^.Tag = Button) then
begin
writeLn(i^.Button.Name);
writeLn(i^.Button.id);
end;
if (i^.Tag = CheckBox) then
begin
writeLn(i^.CheckBox.Name);
writeLn(i^.CheckBox.id);
end;
i := I^.Next;
end;
end.
теперь проблемы с writeln
Error: Can't read or write variables of this type
Ошибка: не удается прочитать или записать переменные этого типа
Объясните в чём проблема.