Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Verständnisfrage Deklaration (https://www.delphipraxis.net/155021-verstaendnisfrage-deklaration.html)

gmc616 5. Okt 2010 14:10


Verständnisfrage Deklaration
 
Hallo DP.

Wie ist so eine Deklaration zu verstehen?

Delphi-Quellcode:
// aus Types.pas

  TRect = packed record
    case Integer of
      0: (Left, Top, Right, Bottom: Longint);
      1: (TopLeft, BottomRight: TPoint);
  end;
:?:


Danke, gmc

mleyen 5. Okt 2010 14:18

AW: Verständisfrage Deklaration
 
Sozusagen sind das folgende 2 Typen zusammengefasst:
Delphi-Quellcode:
  TRect1 = packed record
    Left, Top, Right, Bottom: Longint;
  end;
  TRect2 = packed record
    TopLeft, BottomRight: TPoint;
  end;
Bei case of in einem Record wird jedoch der gleiche Speicherbereich verwendet.
Dh man hat sozusagen einen direkten Zugriff auf einen Cast der definierten Typen.

Hier noch ein Beispielcode zum testen:
Delphi-Quellcode:
type
  TRect = packed record
    case Integer of
      0: (Left, Top, Right, Bottom: Longint);
      1: (BottomRight, TopLeft: TPoint);
  end;
var
  rct: TRect;
begin
  rct.Left := 0;
  rct.Top := 1;
  rct.Right := 2;
  rct.Bottom := 3;
  ShowMessage(
    IntToStr(rct.TopLeft.X) + '/' + IntToStr(rct.TopLeft.Y) + sLineBreak +
    IntToStr(rct.BottomRight.X) + '/' + IntToStr(rct.BottomRight.Y)
  );

DeddyH 5. Okt 2010 14:22

AW: Verständnisfrage Deklaration
 
Das nennt sich varianter Record (um mal einen Suchbegriff zu nennen).


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:52 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz