[fpc-pascal] Empty record inside another record ?

Skybuck Flying skybuck2000 at hotmail.com
Sat Feb 16 18:23:19 CET 2008


What's the benefit of using objects ?

One drawback of objects already discovered:

Objects cannot have the same field identifiers.

A problem that does not exist with records, and empty records.

// object example, pointers and typecasts still necessary.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
 PethernetHeader = ^TethernetHeader;
 TethernetHeader = object
  SomeFields1 : packed array[0..13] of byte;
 end;

 PipHeaderVersion4 = ^TipHeaderVersion4;
 TipHeaderVersion4 = object(TethernetHeader)
  SomeFields2 : packed array[0..19] of byte; // first problem, identifier 
redeclared, numbers used to fix it.
 end;

 TudpHeaderVersion4 = object(TipHeaderVersion4)
  SomeFields3 : packed array[0..7] of byte;
 end;

 TtcpHeaderVersion4 = object(TipHeaderVersion4)
  SomeFields4 : packed array[0..19] of byte;
 end;

 Tframe = object
  SomeFields5 : packed array[0..66000] of byte;
 end;

procedure Main;
var
 frame : Tframe;
begin
 frame.SomeFields5[14] := 234;

// TethernetHeader(frame). // typecast not possible.

// with frame as TethernetHeader do // operator not applicable
// begin
//  SomeFields5[4] := 123;
// end;

 // maybe pointers can help
 writeln( PipHeaderVersion4(@Frame).SomeFields2[0] );
end;

begin
  try
 Main;
  except
 on E:Exception do
   Writeln(E.Classname, ': ', E.Message);
  end;
  readln;
end.

Bye,
  Skybuck. 




More information about the fpc-pascal mailing list