数组重载运算符 [英] Overloading operator with array
问题描述
我有这个单位:
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,例如:
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;
然后我修改添加为:
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 Error] Main。 TIns.pas(31):E2124实例成员'MyVal'在这里无法访问,因此无法理解我的错误。
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.
推荐答案
这是您要执行的操作的一个有效示例:
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.
要拉出数组元素,请声明默认属性Items。
通过属性Count公开元素计数。
并且正如Uwe指出的那样,首先在添加运算符中将结果设置为Elem1。
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.
这篇关于数组重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!