为什么不能对类使用运算符重载? [英] Why can I not use operator overloading for classes?
本文介绍了为什么不能对类使用运算符重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在TDateTime中遇到类运算符的问题.
I'm having a problem with class operators in TDateTime.
type
TDateTime = class(TObject)
public
class operator Add(a: TDateTime; b: TTimeSpan): TDateTime;
class operator Subtract(a: TDateTime; b: TTimeSpan): TDateTime;
end;
implementation
class operator TDateTime.Add(a: TDateTime; b: TTimeSpan): TDateTime;
begin
result := TDateTime.Create(a.Ticks + b.Ticks);
end;
class operator TDateTime.Subtract(a: TDateTime; b: TTimeSpan): TDateTime;
begin
result := TDateTime.Create(a.Ticks - b.Ticks);
end;
错误发生在第四行
E2123预期的过程,功能,属性或VAR
E2123 PROCEDURE, FUNCTION, PROPERTY, or VAR expected
我是为Windows编写的.但是,如果仅适用于.net和iOS,我该如何针对Windows?
I write this for Windows. But if it available only for .net and iOS, how can I do that for Windows?
推荐答案
在Delphi for Windows中,可以仅对记录使用运算符重载,对类不使用.
http://docwiki.embarcadero.com/RADStudio/Rio/en/Operator_Overloading_ (德尔福)
It is possible to use operator overloading only for records, not for classes in Delphi for Windows.
http://docwiki.embarcadero.com/RADStudio/Rio/en/Operator_Overloading_(Delphi)
P.S. TDateTime对于用户类型而言是错误的名字.
P.S. TDateTime is bad name for user type.
这篇关于为什么不能对类使用运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文