delphi – 使用数组重载运算符

我有这个单位:

unit Main.TIns;

interface

type
  TIns = record
  private type
    TInsArray = array [0..90] of Integer;
  var
    FInsArray: TInsArray;
  public
    class operator Implicit(const Value: Integer): TIns;
    class operator Add(const Elem1: TIns; const Elem2: Integer): TIns;
  end;

implementation

class operator TIns.Implicit(const Value: Integer): TIns;
var
  iIndex: Integer;
begin
  if Value = 0 then
    for iIndex := 0 to 90 do Result.FInsArray[iIndex] := 0;
end;

class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns;
begin
  Inc(Result.FInsArray[0]);
  Result.FInsArray[Result.FInsArray[0]] := Elem2;
end;

end.

主要程序是:

program InsMain;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,Main.TIns in 'Main.TIns.pas';

var
 s: TIns;
begin
    s := 0;      // Initialize ins; similar t := [] //
    s := s + 5;  // Add a element,in this case 5 //
    writeln(s[0]); // Read number of element in s,should to be 1 //
end.

问题是我收到此错误:[DCC错误] InsMain.dpr(20):E2149类没有默认属性.
为什么我无法读取数组元素?
我想过要解决添加变量MyVal的问题,这样做:

type
      TIns = record
      private type
        TInsArray = array [0..90] of Integer;
      var
        FInsArray: TInsArray;
      public
        MyVal: TInsArray;
        class operator Implicit(const Value: Integer): TIns;
        class operator Add(const Elem1: TIns; const Elem2: Integer): TIns;
      end;

然后我修改添加所以:

class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns;
begin
  Inc(Result.FInsArray[0]);
  Result.FInsArray[Result.FInsArray[0]] := Elem2;
  MyVal := Result;   // <--- error E2124 here.
end;

和写作:

writeln(s.MyVal[0]);

不返回错误,但在添加时给出错误,写入:[DCC错误] Main.TIns.pas(31):E2124实例成员’MyVal’在这里无法访问,所以不明白我错在哪里.

解决方法

以下是您尝试执行的操作示例:

type
  TIns = record
  private type
    TInsArray = array [0..90] of Integer;
  private var
    FInsArray : TInsArray;
    function GetItem( index : integer) : integer;
    function GetCount : integer;
  public
    class operator Implicit(const Value: Integer): TIns;
    class operator Add(const Elem1: TIns; const Elem2: Integer): TIns;
    property Items[index : integer] : integer read GetItem; default;
    property Count : integer read GetCount;
  end;

function TIns.GetCount: integer;
begin
  Result := Self.FInsArray[0];
end;

function TIns.GetItem(index: integer): integer;
begin
  Result := Self.FInsArray[index];
end;

class operator TIns.Implicit(const Value: Integer): TIns;
var
  iIndex: Integer;
begin
  if Value = 0 then
    for iIndex := 0 to 90 do Result.FInsArray[iIndex] := 0;
end;

class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns;
begin
  Result := Elem1;
  Inc(Result.FInsArray[0]);
  Result.FInsArray[Result.FInsArray[0]] := Elem2;
end;

var
i : integer;
s,s1 : TIns;
begin
    s := 0;      // Initialize ins; similar t := [] //
    s1 := 0;
    s := s + 5;  // Add a element,in this case 5 //
    s1 := s1 + 10;
    for i := 1 to s.Count do
      writeln( 'S[',i,']=',s[i]); // Read element values in s
    for i := 1 to s1.Count do
      writeln( 'S1[',s1[i]); // Read element values in s1
    ReadLn;
end.

要提取数组元素,请声明默认属性Items.通过属性Count公开元素计数.正如Uwe指出的那样,在Add运算符中首先将Result设置为Elem1.

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...