【问题标题】:Class Helper and Strings in Delphi Win32Delphi Win32 中的类助手和字符串
【发布时间】:2009-09-11 06:28:57
【问题描述】:

目前的delphi有什么办法可以实现。

a) 带有运算符重载(即+、=)的字符串(作为一个类)

b) 类助手,因此可以添加自定义字符串方法

我收集到字符串是原生类型,所以没有类助手将无法工作 设置课程等。

【问题讨论】:

  • 具体是哪个版本的Delphi?

标签: delphi class string operator-overloading helpers


【解决方案1】:

是的,字符串是一种原生类型,添加了一些特殊的编译器魔法。

我不知道你想要什么运算符重载。 + 和 = 已经可以用作连接和相等运算符。

不过,我曾想过自己做一些类似的事情。它可能适用于带有隐式转换器和重载的 add 和 equals 运算符的记录类型(在 Win32 Delphi 中,只有记录可以有运算符重载。这仅在 D2006 (?2005) 中可用。)

我怀疑性能也会受到影响。

语法如下:

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.

显然你也可以有“记录助手”,但我自己从未尝试过。

【讨论】:

  • 你也可以创建记录助手。
  • 如果将记录通过赋值复制到另一条记录,是不是会出现字符串引用计数问题?
【解决方案2】:

在 Delphi(自 Delphi 2006 起)中,您只能对不在类上的记录使用运算符重载,而不能对字符串等内置本机类型使用。

原因是Delphi没有垃圾回收,所以运算符重载仅限于值类型(不在堆上的类型)。

您可以在CodeRage III Replay download page 下载我的会话“具有记录、方法和运算符重载的可空类型”的回放。 只需搜索会话名称即可。

还有一个page with the download for the session samples and slides

它包含很多示例,可以帮助您继续前进,包括 Delphi 2006 编译器中已在 Delphi 2007 及更高版本中解决的一些问题的描述。

另请参阅此问题:Can I overload operators for my own classes in Delphi?

【讨论】:

    【解决方案3】:

    编写自定义函数/过程不是更好的解决方案吗?

    例如

    Function StrToBase64(AString): string;
    Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
    Function ExtractWord(aString): string;
    Function GetStrColumn(aString: string; aCol: integer): string;
    

    如果您想将这些驻留在同一单元中的功能/过程分组到功能类别中,您可以为此使用记录:

    TStringConversions = record
      class Function StrToBase64(AString): string;
      class Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
    end;
    
    TStringParsing = record
      class Function ExtractWord(aString): string;
      class Function GetStrColumn(aString: string; aCol: integer): string;
    end;
    

    然后,您可以以更简洁的方式在代码中调用它们:

    myFirstWord := TStringParsing.ExtractWord('Delphi is a very good tool');
    

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多