PDA

View Full Version : Adding an operator to TPoint



pstudio
14-10-2007, 07:23 PM
Hey, is it possible to add class operators to existing delphi types? To be more specific I would like to add the 'add' operator to the TPoint record.

Ñuño Martínez
14-10-2007, 08:32 PM
You can do it in a new class that inherits TPoint. Never used operator overloading in Pascal (I was traumatised when I used C++ :batman: and I never needed them since I use Pascal) but would be something like:CLASS TMyPoint = CLASS (TPoint);
PUBLIC
OPERATOR + (Sum: TMyPoint): TMyPoint; VIRTUAL;
END;

ETCETERA... :think: I know that Free Pascal allows operator overloading, does Delphi allow this too?

User137
14-10-2007, 08:42 PM
Delphi 7 doesn't look like it even knows word operator. Editor doesn't also bold it like happens with function or procedure.

pstudio
14-10-2007, 08:42 PM
The syntax is different in Delphi and you can only implement some predefined operators.

I'm not interesting in declaring my own type since I'm gonna use the points in some drawing operations. I tried making my own point record but then the drawing procedure wouldn't accept it. :(

JSoftware
15-10-2007, 09:32 AM
You can't extend it but you can make a transfer type like this:

type
TMyPoint = record
fpoint: TPoint;
class operator add(a, b: TMyPoint): TMyPoint;
class operator implicit(a: TMyPoint): TPoint;
class operator implicit(a: TPoint): TMyPoint;
END;

class operator TMyPoint.add(a, b: TMyPoint): TMyPoint;
begin
result.fpoint.x := a.fpoint.x+b.fpoint.x;
result.fpoint.y := a.fpoint.y+b.fpoint.y;
end;

class operator implicit(a: TMyPoint): TPoint;
begin
result =: a.fpoint;
end;

class operator implicit(a: TPoint): TMyPoint;
begin
result.fpoint := a;
end;

Now you can use it like this:

var a,b: tmypoint;
c: tpoint;
begin
a := point(2,5);
b := point(5,7);
a := a+b;
c := a;
end;


Since you are danish I can point you toward my article on OO in delphi http://udvikleren.dk/Delphi/Article.aspx/297/ :P

{MSX}
15-10-2007, 10:16 AM
never saw that kind of code before.. so you can define class functions on record types? very interesting :)
how does the "implicit" operator work?

User137
15-10-2007, 10:43 AM
Or, drawing procedure will accept with typecast as long as record contains same or same length information as the other:

type
TMyPoint = record
x,y: integer;
end;

var p: TMyPoint;
begin
...Draw(TPoint(p), ...);
end;

JSoftware
15-10-2007, 02:10 PM
You can declare all kind of methods for normal records in Delphi now.

The implicit operator is called when you try to assign a type to another like this:


var a: type1;
b: type2;
begin
a := b;
end;


Here either class operator type1.implicit(right: type2): type1; or type2.implicit(right: type2): type1; will be called

Explicit will be called when you try to cast a type to another in the same style. Naturally explicit will be easier to control and predict but it'll be more work using it

pstudio
15-10-2007, 02:12 PM
Thanks guys.
I made it work by using my own point record and some implicit and explicit typecasts :D



Since you are danish I can point you toward my article on OO in delphi http://udvikleren.dk/Delphi/Article.aspx/297/ Razz
I've already read it ;) It was the first article I read about OO after I heard of it :D


how does the "implicit" operator work?
It allows you to convert one type to an other without explicit writing a typecast. The last line in JSoftwares exemple demonstrates this.

{MSX}
16-10-2007, 08:45 PM
You can declare all kind of methods for normal records in Delphi now.

The implicit operator is called when you try to assign a type to another like this:


var a: type1;
b: type2;
begin
a := b;
end;


Here either class operator type1.implicit(right: type2): type1; or type2.implicit(right: type2): type1; will be called

Explicit will be called when you try to cast a type to another in the same style. Naturally explicit will be easier to control and predict but it'll be more work using it

wow great! look like i'm a little behind :)