Operator Overloading in Delphi

半腔热情 提交于 2019-12-14 00:49:39

问题


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;

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

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

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.

Regars, Artik


回答1:


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.

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 has blogged about this: http://blog.marcocantu.com/blog/class_operators_delphi.html



来源:https://stackoverflow.com/questions/17963909/operator-overloading-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!