Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Operatoren Implizit und Explizit (https://www.delphipraxis.net/76766-operatoren-implizit-und-explizit.html)

Eichhoernchen 9. Sep 2006 11:18


Operatoren Implizit und Explizit
 
Hi,

kann es sein, dass die explizite Typenumwandlung nicht richtig klappt?

habe:

Delphi-Quellcode:
  TBruch = record
    z, n: integer;
    class operator Explicit(a: TBruch): string;
    class operator Implicit(a: TBruch): string;
  end;

...

class operator TBruch.Explicit(a: TBruch): string;
begin
  if a.z > a.n then
    result := IntToStr(a.z div a.n) + '+' + IntToStr(a.z mod a.n) + '/' + IntToStr(a.n)
  else
    result := IntToStr(a.z) + '/' + IntToStr(a.n);
end;


class operator TBruch.Implicit(a: TBruch): string;
begin
  result := IntToStr(a.z) + '/' + IntToStr(a.n);
end;

...
wenn ich später dann im Code schreibe,
dass ich ne explizite typenumwandlung will, führt er immer ne implizite durch... woran liegt das?

explizit:
Delphi-Quellcode:
s := string(b);
implizit:
Delphi-Quellcode:
s := b;
(s ist ein string und b ist vom typ TBruch)

in beiden Fällen wird die Implicit funktion, oder wie auch immer man so nen Ding nennt, was nen Operator überlädt ausgeführt!


*help*


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz