数组重载运算符

编程入门 行业动态 更新时间:2024-10-25 10:25:42
本文介绍了数组重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个单位:

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.

更多推荐

数组重载运算符

本文发布于:2023-06-03 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!