Sign in Join

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

Mailing lists / fpc-pascal / 2000-10

Operator overloading

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Sebastian Schuberth

Hi all,

operator overloading does not seem to work as methods of objects / classes. Is there any plan to change that?

Sebastian Schuberth
Florian Klaempfl
to Sebastian Schuberth
Sebastian Schuberth wrote 6 lines
Hi all, operator overloading does not seem to work as methods of objects / classes. Is there any plan to change that?

Though C++ saupports this, I don't see a real use of this!

Sebastian Schuberth
to Florian Klaempfl
Florian Klaempfl wrote
> operator overloading does not seem to work as methods of objects / > classes. Is there any plan to change that? Though C++ saupports this, I don't see a real use of this!

Well, I think it's just a nice feature. E.g. if you already have a complex number class (or vector class or ...) it would be nice to improve its useability by adding operators instead of writing z:=z.add(w) whole the time. Besides, I do not consider it to be good coding style to implement complex numbers as records and some functions. That's IMHO exactly what OOP is good for.

Sebastian Schuberth
Sebastian Schuberth
to Sebastian Schuberth

Hi all,

I just realized that it is possible to define operators via overloading which accept objects as parameters! You just have to define those function outside of the object scope. My mistake was to try to write "operator +(a,b:Complex) ...", just like any other object member function, within the object definition. Hey, Free Pascal is great :-)

Sebastian Schuberth
Florian Klaempfl
to Sebastian Schuberth
Sebastian Schuberth wrote 8 lines
Hi all, I just realized that it is possible to define operators via overloading which accept objects as parameters! You just have to define those function outside of the object scope. My mistake was to try to write "operator +(a,b:Complex) ...", just like any other object member function, within the object definition. Hey, Free Pascal is great :-)

Hmmm, operator overloading works always global so the overloaded operator can't be defined in a object. Sorry, I misunderstood your first question.

Unknown author
to Sebastian Schuberth: I just realized that it is possible to define operators via overloading which accept…

Someone alluded earlier to overloading operators in FPC... could I see source for that?

Sorry for such a trivial question! I'm a novice.

Rach.

________________________________________________________________ YOU'RE PAYING TOO MUCH FOR THE INTERNET!
Juno now offers FREE Internet Access!
Try it today - there's no risk! For your FREE software, visit: http://dl.www.juno.com/get/tagj.

Sebastian Schuberth
to Unknown author
Someone alluded earlier to overloading operators in FPC... could I see source for that?

This is the test program I used to see whether operator overloading work with objects:

--8<--(BEGIN fractal.pas)--
Program Fractal;
Uses
  CRT;
Const
  cr0 = -2.5;
  cr1 = +1.5;
  ci0 = +1.5;
  ci1 = -1.5;
Type
  Float=Single;
  Complex=Object
    Private
    re,im:Float;
    Public
    Function getRadius:Float;
    Function toString:String;
  End;
Function Complex.getRadius:Float;
Begin
  getRadius:=Sqrt(re*re+im*im);
End;
Function Complex.toString:String;
Var
  reStr,imStr:String;
Begin
  Str(re:0:4,reStr);
  Str(im:0:4,imStr);
  toString:='<complex re='+reStr+', im='+imStr+'>';
End;
Operator +(a,b:Complex)z:Complex;
Begin
  z.re:=a.re+b.re;
  z.im:=a.im+b.im;
End;
Operator *(a,b:Complex)z:Complex;
Begin
  z.re:=a.re*b.re-a.im*b.im;
  z.im:=a.re*b.im+a.im*b.re;
End;
Var
  z,c:Complex;
  crInc,ciInc:Float;
  x,y,Count:LongInt;

Begin
Asm
mov eax,13h
int 10h

  End;
  crInc:=(cr1-cr0)/319;
  ciInc:=(ci1-ci0)/199;
  c.im:=ci0;
  For y:=0 To 199 Do Begin
    c.re:=cr0;
    For x:=0 To 319 Do Begin
      z.re:=0;
      z.im:=0;
      Count:=31;
      Repeat
        z:=z*z+c;
        Inc(Count);
      Until (z.getRadius>=2) Or (Count=255);
      Mem[$A0000+y*320+x]:=Count;
      c.re+=crInc;
    End;
    c.im+=ciInc;
  End;
  ReadKey;

Asm
mov eax,03h
int 10h

  End;

End.
--8<--(END fractal.pas)--

Sebastian Schuberth