Sign in Join

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

Mailing lists / fpc-devel / 2003-11

Operator overloading question.

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

BiPolar

Hello.

While trying to use the operator overload feature of FPC, I find out a restriction not mentioned on the documentation. One can overload several operators but not the comparison one (=); this, at least, while operating on classes.

The code below shows this situation (commenting out the "=" operator gives no error)

Please, disregard this message if this is "as it should be". But I think that a small paragraph mentioning this "issue" could be included on the docs.

Thanks for your work.

Oscar Lesta. (not on the mail list, please CC any reply)

----------------------------------------------------------------------
unit foo;
interface
type
  TSomeClass = class
    value1, value2 : Byte;
  end;
operator + (const a, b : TSomeClass) : TSomeClass;
operator = (const a, b : TSomeClass) : Boolean;
implementation
operator + (const a, b : TSomeClass) : TSomeClass;
begin
  Result.x := a.value1 + b.value1;
  Result.y := a.value2 + b.value2;
end;
operator = (const a, b : TSomeClass) : Boolean;
begin
  Result := (a.value1 = b.value1) and (a.value2 = b.value1);
end;
end.
----------------------------------------------------------------------

Reply by email

Peter Vreman
to BiPolar
Show quoted text 13 lines
Hello. While trying to use the operator overload feature of FPC, I find out a restriction not mentioned on the documentation. One can overload several operators but not the comparison one (=); this, at least, while operating on classes. The code below shows this situation (commenting out the "=" operator gives no error) Please, disregard this message if this is "as it should be". But I think that a small paragraph mentioning this "issue" could be included on the docs.

class=class operatorion is handled by the compiler and can't be overloaded. Allowing this as overloaded operation will lead to ambigious statements like

if (someclass=someclass)

what do you want to compare? the address of the class or the contents of the class (like you overloaded example). And how to handle child classes? Do the need to be implicitly cast to tsomeclass first or do you want the pointer comparison only.

Reply by email

BiPolar
to Peter Vreman
Peter Vreman wrote
class=class operatorion is handled by the compiler and can't be overloaded.

Clear as water. Maybe you could include this explanation on the docs, so people like me (new to operator overloading) doesn't ask again.

Show quoted text 7 lines
what do you want to compare? the address of the class or the contents of the class (like you overloaded example). And how to handle child classes? Do the need to be implicitly cast to tsomeclass first or do you want the pointer comparison only.

As I'm working on a pascal wrapper for a C++ API, I wanted to "emulate" as close as possible the C++ classes. And as some of those classes have some operators overloaded, I wanted to do the same on pascal. Of course, C++ operators where overloaded _inside_ of those classes.

Well, I'll have to wait for an "operator TMyClass.= (a, b : TMyClass) : Boolean;" :-)

Thanks.

Oscar Lesta.

Reply by email