[Lazarus] operator overloading
Sven Barth
pascaldragon at googlemail.com
Sun Feb 24 16:04:01 CET 2013
On 24.02.2013 08:31, xrfang wrote:
> Hi All,
>
> How can I use operator overloading? I know the syntax, but it simply
> does not compile... My situation is:
>
> 1. I wrote a TTreap generic class, which is a binary tree.
> 2. TTreap defines a Compare() with involves < and > operation on its keys.
> 3. I try to specialize it with the following:
>
> ===================================================
> interface
>
> TPainterManager = class(specialize TTreap<TPainter, Integer>)
> public
> class operator < (p1, p2: TPainter) b: Boolean; <-- method1
> end;
>
> operator < (p1, p2: TPainter) b: Boolean; <-- method2
>
> implementation
> ...
> ===================================================
>
> but neither method1 nor method2 compiles.
>
> What is the correct way to write operator overload routines?
There is only one way how you can solve this: the type TPainter MUST be
a record and there you can define the operator using "class operator <"
and "class operator >" (in mode ObjFPC you need to use {$modeswitch
advancedrecords} as well).
Maybe one can think about allowing operators in other structured types
(classes, objects, interfaces) as well...
Regards,
Sven
More information about the Lazarus
mailing list