Delphi 如何避免类的未记录运算符重载导致内存泄漏?

Delphi 如何避免类的未记录运算符重载导致内存泄漏?,delphi,operator-overloading,undocumented-behavior,Delphi,Operator Overloading,Undocumented Behavior,Delphi有许多已记录的运算符重载: 用于和(在NexGen编译器中)。 正式来说,它不支持类、接口或简单类型的运算符重载 除了 在接口上工作,并且。 (尽管未登记的操作员似乎不处理记录) 例如,以下代码将编译并运行: Win32/64中类的运算符重载 //sorry for the horrible use of `or` to use for nil testing. //I'd code this into a function (function FirstAssigned(a,b:

Delphi有许多已记录的运算符重载:

用于和(在NexGen编译器中)。
正式来说,它不支持类、接口或简单类型的运算符重载

除了

在接口上工作,并且。
(尽管未登记的操作员似乎不处理记录)

例如,以下代码将编译并运行:

Win32/64中类的运算符重载

//sorry for the horrible use of `or` to use for nil testing.
//I'd code this into a function (function FirstAssigned(a,b: TObject): TObject;
//It's just an example to demonstrate the concept. 
type
  TObjectHelper = class helper for TObject
  public
    class function &&op_LogicalOr<T: class>(A, B: T): T; static;
  end;

class function TObjectHelper.&&op_LogicalOr<T>(A, B: T): T;
begin
  if A <> nil then
    Result := A
  else
    Result := B;
end;

procedure Test;
var
  sl1, sl2, sl3: TStringList;
begin
  sl1 := nil;
  sl2 := TStringList.Create;
  sl3 := sl1 or sl2; // -> sl3 = sl2
end;
//很抱歉在nil测试中使用了`or`to。
//我会把它编码成一个函数(functionfirstassigned(a,b:TObject):TObject;
//这只是一个例子来说明这个概念。
类型
TObjectHelper=TObject的类帮助器
公众的
类函数&运算逻辑(A,B:T):T;静态;
结束;
类函数TObjectHelper.&op_LogicalOr(A,B:T):T;
开始
如果是零那么
结果:=A
其他的
结果:=B;
结束;
程序测试;
变量
sl1、sl2、sl3:TStringList;
开始
sl1:=nil;
sl2:=TStringList.Create;
sl3:=sl1或sl2;//->sl3=sl2
结束;
以下是所有允许操作员的列表,以供参考:

class operator      class function          example
---------------------------------------------------
Implicit            &&op_Implicit           x:= y;
Explicit            &&op_Explicit           x:= integer(y);
Negative            &&op_UnaryNegation      x:= -y
Positive            &&op_UnaryPlus          x:= +y
Inc                 &&op_Increment          Inc(x);
Dec                 &&op_Decrement          Dec(y);
LogicalNot          &&op_LogicalNot         Not(y); //can be used for bitwise not as well
Trunc               &&op_Trunc              i:= trunc(f);
Round               &&op_Round              i:= round(f);
In                  &&op_In                 if (i in s) then
Equal               &&op_Equality           a = b
NotEqual            &&op_Inequality         a <> b
GreaterThan         &&op_GreaterThan        a > b
GreaterThanOrEqual  &&op_GreaterThanOrEqual a >= b 
LessThan            &&op_LessThan           a < b
LessThanOrEqual     &&op_LessThanOrEqual    a <= b
Add                 &&op_Addition           a + b
Subtract            &&op_Subtraction        a - b
Multiply            &&op_Multiply           a * b
Divide              &&op_Division           a / b //floating point div
IntDivide           &&op_IntDivide          a div b //integer div
Modulus             &&op_Modulus            a mod b
LeftShift           &&op_LeftShift          a shl b
RightShift          &&op_RightShift         a shr b
LogicalAnd          &&op_LogicalAnd         if (a and b) then ...
LogicalOr           &&op_LogicalOr          if (a or b) then ....
LogicalXor          &&op_ExclusiveOr        if (a xor b) then
BitwiseAnd          &&op_BitwiseAnd         x:= a and b
BitwiseOr           &&op_BitwiseOr          x:= a or b
BitwiseXor          &&op_BitwiseXOR         x:= a xor b
Include             &&op_Include            include(s, i);
Exclude             &&op_Exclude            exclude(s, i);
类运算符类函数示例
---------------------------------------------------
隐式&op_隐式x:=y;
显式&op_显式x:=整数(y);
否定与运算单否定x:=-y
正和负一元加x:=+y
Inc和op_增量Inc(x);
12月&op_减量12月(y);
LogicalNot&&op_LogicalNot Not(y);//也可用于按位Not
Trunc和op_Trunc i:=Trunc(f);
第一轮和第二轮:=第(f)轮;
In&&op_In if(i In s)then
相等&op_相等a=b
NotEqual&op_不等式a b
大于&op_大于a>b
更高的质量a>=b
乐山&乐山aLessthanRequal&&op(LessthanRequal a我知道你的意思,因为我看到了促使这一点的Google+讨论。但是没有任何上下文或理由,这个问题毫无意义。这里有一个很好的问答,但这不是。请不要发布半格式的内容。花时间充实一下。@Johan我没有认为这个问题目前非常适合Stack Overflows问答风格-你能把它缩小到你面临的实际问题吗?参见例如(你应该根据你面临的实际问题提出实际的、可回答的问题。闲聊的、开放式的问题会降低我们网站的有用性,并将其他问题从首页上推掉。)有一点。通过手动销毁由某个运算符创建的任何对象,可以避免内存泄漏。实际上,这是不切实际的。显然,返回值类型的运算符会很好。@DavidHeffernan我想说,如果Delphi实现两个功能,这可能是可行的。@Records construct触发器/析构函数(或初始化器/终结器)-长时间丢失的功能;以及自动将这些类/简单类型装箱/拆箱到records@Arioch'使用这些记录执行A:=B时会发生什么情况。现在您有两个对同一对象的引用。装箱通常以另一种方式完成。将值类型转换为引用类型。我知道您的意思,因为我看到了他在Google+讨论中提出了这一点。但没有任何上下文或理由,这个问题毫无意义。这里有一个很好的问答,但这不是。请不要发布半格式的内容。花时间充实一下。@Johan我认为这个问题目前不适合堆栈溢出问答风格-你能缩小范围吗如何回答你所面临的实际问题?参见(你应该根据你所面临的实际问题提出实际的、可回答的问题。闲聊的、开放式的问题会降低我们网站的有用性,并将其他问题从头版推掉。)有一点。通过手动销毁由某个运算符创建的任何对象,可以避免内存泄漏。实际上,这是不切实际的。显然,返回值类型的运算符会很好。@DavidHeffernan我想说,如果Delphi实现两个功能,这可能是可行的。@Records constructtors/析构函数(或初始化器/终结器)-一个长时间丢失的特性;以及将这些类/简单类型自动装箱/拆箱到这些类中records@Arioch'使用这些记录执行A:=B时会发生什么情况。现在您有两个对同一对象的引用。装箱通常以另一种方式完成。将值类型转换为引用类型。