Results 1 to 10 of 10

Thread: Adding an operator to TPoint

  1. #1

    Adding an operator to TPoint

    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

  2. #2

    Adding an operator to TPoint

    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.

  3. #3

    Adding an operator to TPoint

    Delphi 7 doesn't look like it even knows word operator. Editor doesn't also bold it like happens with function or procedure.

  4. #4

    Adding an operator to TPoint

    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

  5. #5

    Adding an operator to TPoint

    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

  6. #6

    Adding an operator to TPoint

    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>

  7. #7

    Adding an operator to TPoint

    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]

  8. #8

    Adding an operator to TPoint

    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

  9. #9

    Adding an operator to TPoint

    Thanks guys.
    I made it work by using my own point record and some implicit and explicit typecasts

    Code:
    Since you are danish I can point you toward my article on OO in delphi http&#58;//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

    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.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  10. #10

    Adding an operator to TPoint

    Quote Originally Posted by JSoftware
    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
    wow great! look like i'm a little behind
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •