Einzelnen Beitrag anzeigen

Benutzerbild von gubbe
gubbe

Registriert seit: 8. Okt 2005
Ort: Schleswig-Holstein
74 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Zugriffsverletzung bei Zeiger auf statisches Array

  Alt 26. Aug 2019, 10:42
Warum nicht klassisch objektorientiert, ganz ohne hässliche Pointer?

Code:

unit Unit1;

interface

type
  TMyRecord = record
    A: Integer;
    B: Integer;
    C: Byte;
  end;

  TMyClass = class
  private
  protected
    function GetRecord(const AIndex: Integer): TMyRecord; virtual; abstract;
  public
    property Records[const AIndex: Integer]: TMyRecord read GetRecord; default;
  end;

  TMyClassA = class(TMyClass)
  strict private const
    RECS: array[0..1] of TMyRecord = (
      (A: 1; B: 2; C: 3),
      (A: 4; B: 5; C: 6)
    );
  protected
    function GetRecord(const AIndex: Integer): TMyRecord; override;
  public
  end;

  TMyClassB = class(TMyClass)
  strict private const
    RECS: array[0..2] of TMyRecord = (
      (A: 1; B: 2; C: 3),
      (A: 4; B: 5; C: 6),
      (A: 7; B: 8; C: 9)
    );
  protected
    function GetRecord(const AIndex: Integer): TMyRecord; override;
  end;

implementation

function TMyClassA.GetRecord(const AIndex: Integer): TMyRecord;
begin
  result := Recs[AIndex];
end;

function TMyClassB.GetRecord(const AIndex: Integer): TMyRecord;
begin
  result := Recs[AIndex];
end;

end.
Und so kannst Du auch die Übergabeparameter noch prüfen.
  Mit Zitat antworten Zitat