Oui, chaîne est un type natif avec un peu de magie de compilateur spécial ajouté.
Je ne sais pas quelle surcharge d'opérateur vous voudriez. + et = fonctionnent déjà en tant qu'opérateurs de concaténation et d'égalité.
Cependant, j'ai pensé à faire quelque chose de similaire moi-même. Il peut fonctionner avec un type d'enregistrement avec des convertisseurs implicites et des opérateurs d'addition et d'égal surchargés (dans Win32 Delphi, seuls les enregistrements peuvent être surchargés par l'opérateur) (D2006 (2005) uniquement.)
Je suppose qu'il peut y avoir des performances frappé aussi bien.
La syntaxe serait quelque chose comme ce qui suit:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString = record
private
Value : string;
public
class operator Add(a, b: TString): TString;
class operator Implicit(a: Integer): TString;
class operator Implicit(const s: string): TString;
class operator Implicit(ts: TString): String;
function IndexOf(const SubStr : string) : Integer;
end;
var
Form1: TForm1;
implementation
class operator TString.Add(a, b : TString) : TString;
begin
Result.Value := a.Value + b.Value;
end;
class operator TString.Implicit(a: Integer): TString;
begin
Result.Value := IntToStr(a);
end;
class operator TString.Implicit(ts: TString): String;
begin
Result := ts.Value;
end;
function TString.IndexOf(const SubStr : string) : Integer;
begin
Result := Pos(SubStr, Value);
end;
class operator TString.Implicit(const s: string): TString;
begin
Result.Value := s;
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ts : TString;
begin
ts := '1234';
ShowMessage(ts);
ShowMessage(IntToStr(Ts.IndexOf('2')));
end;
end.
Apparemment, vous pouvez avoir des « aides record », ainsi, mais je ne l'ai jamais essayé moi-même.
Quelle version de Delphi spécifiquement? –