6

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

Artik
  • 803
  • 14
  • 36

1 Answers1

10

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

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490