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.
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.
Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits
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:[pascal]CLASS TMyPoint = CLASS (TPoint);
PUBLIC
OPERATOR + (Sum: TMyPoint): TMyPoint; VIRTUAL;
END;
ETCETERA...[/pascal] :think: I know that Free Pascal allows operator overloading, does Delphi allow this too?
No signature provided yet.
Delphi 7 doesn't look like it even knows word operator. Editor doesn't also bold it like happens with function or procedure.
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.
Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits
You can't extend it but you can make a transfer type like this:
[pascal]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;[/pascal]
Now you can use it like this:
[pascal]
var a,b: tmypoint;
c: tpoint;
begin
a := point(2,5);
b := point(5,7);
a := a+b;
c := a;
end;
[/pascal]
Since you are danish I can point you toward my article on OO in delphi http://udvikleren.dk/Delphi/Article.aspx/297/
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
never saw that kind of code before.. so you can define class functions on record types? very interesting
how does the "implicit" operator work?
If you save your data in a proprietary format, the owner of the format owns your data.
<br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>
Or, drawing procedure will accept with typecast as long as record contains same or same length information as the other:
[pascal]type
TMyPoint = record
x,y: integer;
end;
var p: TMyPoint;
begin
...Draw(TPoint(p), ...);
end;[/pascal]
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:
[pascal]
var a: type1;
b: type2;
begin
a := b;
end;
[/pascal]
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
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
Thanks guys.
I made it work by using my own point record and some implicit and explicit typecasts
I've already read it It was the first article I read about OO after I heard of itCode:Since you are danish I can point you toward my article on OO in delphi http://udvikleren.dk/Delphi/Article.aspx/297/ Razz
It allows you to convert one type to an other without explicit writing a typecast. The last line in JSoftwares exemple demonstrates this.how does the "implicit" operator work?
Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits
wow great! look like i'm a little behindOriginally Posted by JSoftware
If you save your data in a proprietary format, the owner of the format owns your data.
<br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>
Bookmarks