Delphi中的运算符重载 [英] Operator Overloading in Delphi

查看:124
本文介绍了Delphi中的运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能(在Delphi中)重载类中的运算符. 我已经读过一段时间了,它只能用于记录,但是我发现类的信息也像下面的代码一样:

is it possible (in Delphi) to overload operators in classes. I've read some time ago it is possible only for records but I found information that for classes too like in code below:

type
   TMyClass = class
     class operator Implicit(a: Integer): TMyClass;
   end;


class operator TMyClass.Implicit(a: Integer): TMyClass;
begin
   // ...
end;

来自(修改)地址: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/operatoroverloads_xml.html

It is (modified) from address: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/operatoroverloads_xml.html

但是当我尝试使用它(在Delphi XE内部)时,我得到了:

But when I try to use it (inside Delphi XE) I get:

预期的过程,功能,属性或VAR(E2123)

PROCEDURE, FUNCTION, PROPERTY, or VAR expected (E2123)

我想创建自己的简单类来进行矩阵操作,并且在类内部使用重载运算符的可能性是非常可观的机会.

I want to create my own simple class for matrix manipulating and possibility of using overloading opearators inside class is very expected opportunity.

瑞格斯, Artik

Regars, Artik

推荐答案

某些版本的编译器提供类的运算符重载.它适用于.net和iOS编译器.不支持Windows和Mac.

Operator overloading for classes is available in some versions of the compiler. It is available for the .net and iOS compilers. For Windows and Mac is is not supported.

iOS编译器可以支持此操作,因为它使用ARC管理类实例的生命周期.如果桌面编译器曾经切换到ARC,那么您可以期望支持操作员重载.

The iOS compiler can support this because it manages lifetime of class instances using ARC. If the desktop compilers ever switch to ARC then you can expect support for operator overloading.

马可(Marco)对此事发表了博客: http://blog.marcocantu.com/blog/class_operators_delphi.html

Marco has blogged about this: http://blog.marcocantu.com/blog/class_operators_delphi.html

这篇关于Delphi中的运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆