Einzelnen Beitrag anzeigen

Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.359 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: E2070 unbekannte Direktive 'operator'

  Alt 20. Feb 2022, 15:33
Ich kenne die Syntax in FreePascal nicht, aber in Delphi sieht das so aus:
Delphi-Quellcode:
type
  TExample = record
  private
    FValue: Integer;
  public
    class operator Add(const AValue1, AValue2: TExample): TExample;
    class operator Implicit(const AValue: Integer): TExample;
    class operator Implicit(const AValue: TExample): Integer;
    class operator Implicit(const AValue: TExample): String;
  end;

{ TExample }

class operator TExample.Add(const AValue1, AValue2: TExample): TExample;
begin
  Result.FValue := AValue1.FValue + AValue2.FValue;
end;

class operator TExample.Implicit(const AValue: TExample): Integer;
begin
  Result := AValue.FValue;
end;

class operator TExample.Implicit(const AValue: TExample): String;
begin
  Result := AValue.FValue.ToString;
end;

class operator TExample.Implicit(const AValue: Integer): TExample;
begin
  Result.FValue := AValue;
end;

// Beispiel:
var
  Test: TExample;
begin
  Test := 40;
  ShowMessage(Test + 2);
end;
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat