Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-02

operator overloading

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

xrfang

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?

Thanks,
Shannon ?I?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130224/6abefd4a/attachment.html>

Reply by email

Sven Barth
to Wildfire
Wildfire wrote 8 lines
*From:* xrfang <mailto:> *Sent:* Sunday, February 24, 2013 7:31 AM *To:* <mailto:> *Subject:* [Lazarus] operator overloading > operator < (p1, p2: TPainter) b: Boolean; <?method2 Close, but no cigar... operator < (p1, p2: TPainter): Boolean;

Both variants will work. The variant with "b: Boolean" was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well.

Regards,
Sven

Reply by email

xrfang
to Sven Barth

I will test again tomorrow to find out what's wrong with my code. ?For now, one more queston:

I noticed that generics does not work without {$mode objfpc}. ?I wonder what is the default mode without $mode? Why objfpc is not the default mode?

? ?, 2? 24, 2013 at 11:00 ???Sven Barth ???

Wildfire wrote 8 lines
*From:* xrfang *Sent:* Sunday, February 24, 2013 7:31 AM *To:* *Subject:* [Lazarus] operator overloading > operator < (p1, p2: TPainter) b: Boolean; <?method2 Close, but no cigar... operator < (p1, p2: TPainter): Boolean;

Both variants will work. The variant with "b: Boolean" was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well.

Regards,
Sven

Reply by email

Sven Barth
to xrfang: How can I use operator overloading? ?I know the syntax, but it simply does not compile...…
xrfang wrote 26 lines
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

Reply by email

Wildfire
to Sven Barth: Both variants will work. The variant with "b: Boolean" was originally introduced for the…
Show quoted text 6 lines
Both variants will work. The variant with "b: Boolean" was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well.

Thanks for the Heads Up Sven, you learn something new everyday.

My apologies for the misinformation (and HTML) earlier. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130224/861cae87/attachment.html>

Reply by email

Sven Barth
to xrfang: I will test again tomorrow to find out what's wrong with my code. ?For now, one more…
xrfang wrote 5 lines
I will test again tomorrow to find out what's wrong with my code. For now, one more queston: I noticed that generics does not work without {$mode objfpc}. I wonder what is the default mode without $mode? Why objfpc is not the default mode?

The default mode is "fpc" and it will stay the default mode because of backwards compatibility. The difference between the two modes is that "fpc" is a bit more like "tp" (no classes, only objects) but with additional features. Generics will work for objects, records, arrays and procedural types in that mode.

Regards,
Sven

Reply by email

xrfang
to leledumbo: FPC operator overloading doesn't work inside classes, it must be declared globally, so…

I confirm that it does not work. ?Test program below, and the class source is attached.

==Test Program===================

program test;
{$mode objfpc}{$H+}
uses
? Classes, treap;
type
? TSLCounter = specialize TTreap<TStringList, Integer>;
operator <(sl1, sl2: TStringList): Boolean;
begin
? Result := sl1.Text < sl2.Text;
end;
begin
end.

==Error Message==================
Options changed, recompiling clean with -B
/home/xrfang/git/fpcollection/src/units/treap.pas(216,10) Error: Operator is not overloaded: "TStringList" < "TStringList" /home/xrfang/git/fpcollection/src/units/treap.pas(218,15) Error: Operator is not overloaded: "TStringList" > "TStringList" test.lpr(19) Fatal: There were 2 errors compiling module, stopping =================================

i.e. whether the operator overloading is defined or not, error message is same.

Thanks.

? ?, 2? 24, 2013 at 4:21 ???leledumbo ???
FPC operator overloading doesn't work inside classes, it must be declared globally, so method2 should work. What error do you get? What FPC version do you use?

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-operator-overloading-tp4029399p4029400.html Sent from the Free Pascal - Lazarus mailing list

Reply by email