Overloading operator with array

夙愿已清 提交于 2019-12-23 17:52:08

问题


i have this unit:

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.

And main program is:

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.

The problem is that i receive this error: [DCC Error] InsMain.dpr(20): E2149 Class does not have a default property. Why i can't read element of array? I have thinked to solve add a variable MyVal for example, doing so:

 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;

then i modify add so:

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;

and writing:

writeln(s.MyVal[0]); 

not return error, but give error on Add, writing: [DCC Error] Main.TIns.pas(31): E2124 Instance member 'MyVal' inaccessible here, so not understood where i mistake.


回答1:


Here is a working example of what you are trying to do :

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[',i,']=',s1[i]); // Read element values in s1
    ReadLn;
end.

To pull out array elements, declare the default property Items. Expose the element count via the property Count. And as Uwe pointed out, set the Result to Elem1 first in the Add operator.




回答2:


In TExtracts.Add you never initialize the result with the content of Elem1. That is why you always end with an empty result.

Update: Your change did more harm than good! Now you are writing to a record field inside a class method. This is not possible as the error message is trying to make clear. Let alone that I don't know what MyVal shall be good for.



来源:https://stackoverflow.com/questions/8419266/overloading-operator-with-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!