klu*_*udg 6 delphi operator-overloading increment
Delphi文档说可能会使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.
Run Code Online (Sandbox Code Playgroud)
运营商的签名是错误的.它应该是:
class operator Inc(const A: TMyInt): TMyInt;
Run Code Online (Sandbox Code Playgroud)
要么
class operator Inc(A: TMyInt): TMyInt;
Run Code Online (Sandbox Code Playgroud)
您不能使用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.
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
1
讨论
当超载时,这是一个相当不寻常的操作员.在使用方面,操作员是就地突变.但是,当重载时,它的作用类似于加法运算符,其隐式加数为1.
所以,在这行上面的代码中:
Inc(A);
Run Code Online (Sandbox Code Playgroud)
有效地转化为
A := TMyInt.Inc(A);
Run Code Online (Sandbox Code Playgroud)
然后编译.
如果您想要保持真正的就地变异语义,并避免与此运算符关联的复制,那么我相信您需要使用该类型的方法.
procedure Inc; inline;
....
procedure TMyInt.Inc;
begin
inc(FValue);
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
275 次 |
最近记录: |