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

如何在Delphi中过载Inc(Dec)运算符?

发布时间:2020-12-15 04:35:08 所属栏目:大数据 来源:网络整理
导读:Delphi documentation表示有可能使Inc和Dec运算符过载;我认为没有有效的方法可以做到这一点.以下是尝试重载Inc运算符;一些尝试导致编译错误,一些尝试导致运行时访问冲突(Delphi XE): program OverloadInc;{$APPTYPE CONSOLE}uses SysUtils;type TMyInt = re
Delphi documentation表示有可能使Inc和Dec运算符过载;我认为没有有效的方法可以做到这一点.以下是尝试重载Inc运算符;一些尝试导致编译错误,一些尝试导致运行时访问冲突(Delphi XE):
program OverloadInc;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMyInt = record
    FValue: Integer;
//    class operator Inc(var A: TMyInt);   DCC error E2023
    class operator Inc(var A: TMyInt): TMyInt;
    property Value: Integer read FValue write FValue;
  end;

class operator TMyInt.Inc(var A: TMyInt): TMyInt;
begin
  Inc(A.FValue);
  Result:= A;
end;

type
  TMyInt2 = record
    FValue: Integer;
    class operator Inc(A: TMyInt2): TMyInt2;
    property Value: Integer read FValue write FValue;
  end;

class operator TMyInt2.Inc(A: TMyInt2): TMyInt2;
begin
  Result.FValue:= A.FValue + 1;
end;

procedure Test;
var
  A: TMyInt;

begin
  A.FValue:= 0;
  Inc(A);
  Writeln(A.FValue);
end;

procedure Test2;
var
  A: TMyInt2;
  I: Integer;

begin
  A.FValue:= 0;
//  A:= Inc(A);  DCC error E2010
  Writeln(A.FValue);
end;

begin
  try
    Test;     // access violation
//    Test2;
  except
    on E: Exception do
      Writeln(E.ClassName,': ',E.Message);
  end;
  Readln;
end.

解决方法

操作符的签名是错误的.它应该是:
class operator Inc(const A: TMyInt): TMyInt;

要么

class operator Inc(A: TMyInt): TMyInt;

您不能使用var参数.

这个计划

{$APPTYPE CONSOLE}

type
  TMyInt = record
    FValue: Integer;
    class operator Inc(const A: TMyInt): TMyInt;
    property Value: Integer read FValue write FValue;
  end;

class operator TMyInt.Inc(const A: TMyInt): TMyInt;
begin
  Result.FValue := A.FValue + 1;
end;

procedure Test;
var
  A: TMyInt;
begin
  A.FValue := 0;
  Inc(A);
  Writeln(A.FValue);
end;

begin
  Test;
  Readln;
end.

产生这个输出:

1

讨论

当超载时,这是一个相当不寻常的运算符.在使用方面,运算符是就地突变.但是,当重载时,它的作用类似于加法运算符,其隐式加数为1.

所以,在这行上面的代码中:

Inc(A);

有效地转化为

A := TMyInt.Inc(A);

然后编译.

如果您想要保持真正的就地变异语义,并避免与此运算符关联的复制,那么我相信您需要使用该类型的方法.

procedure Inc; inline;
....
procedure TMyInt.Inc;
begin
  inc(FValue);
end;

(编辑:李大同)

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

    推荐文章
      热点阅读