加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Delphi中重载指定运算符以进行记录

发布时间:2020-12-15 09:28:35 所属栏目:大数据 来源:网络整理
导读:我想制作使用动态数组的记录类型. 使用这种类型的变量A和B我希望能够执行操作A:= B(和其他)并且能够修改A的内容而无需修改B,如下面的剪切代码: type TMyRec = record Inner_Array: array of double; public procedure SetSize(n: integer); class operator
我想制作使用动态数组的记录类型.

使用这种类型的变量A和B我希望能够执行操作A:= B(和其他)并且能够修改A的内容而无需修改B,如下面的剪切代码:

type
      TMyRec = record
        Inner_Array: array of double;
      public
        procedure SetSize(n: integer);
        class operator Implicit(source: TMyRec): TMyRec;
      end;

    implementation

    procedure TMyRec.SetSize(n: integer);
    begin
      SetLength(Inner_Array,n);
    end;

    class operator TMyRec.Implicit(source: TMyRec): TMyRec;
    begin
    //here I want to copy data from source to destination (A to B in my simple example below)
    //but here is the compilator error
    //[DCC Error] : E2521 Operator 'Implicit' must take one 'TMyRec' type in parameter or result type
    end;


    var
      A,B: TMyRec;
    begin
      A.SetSize(2);
      A.Inner_Array[1] := 1;
      B := A;
      A.Inner_Array[1] := 0;
//here are the same values inside A and B (they pointed the same inner memory)

有两个问题:

>当我在TMyRec中不使用覆盖赋值运算符时,A:= B.
意味着A和B(他们的Inner_Array)指向相同的位置
记忆.
>避免问题1)我想重载assign运算符
使用:

class operator TMyRec.Implicit(source:TMyRec):TMyRec;

但是编译器(Delphi XE)说:

[DCC错误]:E2521运算符’隐式’必须在参数或结果类型中采用一个’TMyRec’类型

如何解决这个问题.
我在stackoverflow上阅读了几篇类似的帖子,但是在我的情况下它们不起作用(如果我理解的话).

ARTIK

解决方法

不能重载赋值运算符.这意味着您尝试做的事情是不可能的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读