Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator Overloading in 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;

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

like image 667
Artik Avatar asked Jul 31 '13 07:07

Artik


1 Answers

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

like image 100
David Heffernan Avatar answered Oct 30 '22 02:10

David Heffernan