Cloud737
Cloud737

Reputation: 357

Why isn't operator overloading available for classes in Delphi?

I've always wondered about this for a bit, but why is operator overloading not available for classes in Delphi?

I remember reading an answer once while on the run, and it said it would come in conflict with something, but I can't remember much. As far as I can tell, only the implicit operator may cause a bit of problems since classes are stored on the heap and assigning is actually a copy of the heap address (basically copying pointers).

Upvotes: 12

Views: 5663

Answers (3)

Johan
Johan

Reputation: 76597

You can have operator overloading for classes, but only for the NextGen compiler where classes use ARC.
See: http://blog.marcocantu.com/blog/class_operators_delphi.html

This was introduced in XE5, see: List of Delphi language features and version in which they were introduced/deprecated

Upvotes: 1

Gustavo R. Croscato
Gustavo R. Croscato

Reputation: 71

Mason Wheeler says that it's impossible because intermediate object management.

But according to Embarcadero documentation, class operators where possible with Delphi 2009.

RAD Studio 2009 - Operator Overloading New Delphi language features since Delphi 7

Upvotes: 0

Mason Wheeler
Mason Wheeler

Reputation: 84550

Close. It's because objects are reference types and the memory is managed manually. So if you said myResult := myObject1 + myObject2 + myObject3;, you'd have to create an intermediate object somewhere in there, and there's no code to free it, so you get memory leaks.

Upvotes: 14

Related Questions