From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4a5bab72e3ac47fc X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!134.158.69.22.MISMATCH!in2p3.fr!irazu.switch.ch!switch.ch!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: Dynamich Dispatching... Date: Sun, 03 Oct 2004 19:11:53 +0200 Organization: AdaCL Message-ID: <1511723.0R0PesaRS9@linux1.krischik.com> References: Reply-To: krischik@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1096824847 03 19147 +yuPXS-vypl5k8M 041003 17:34:07 X-Complaints-To: usenet-abuse@t-online.de X-ID: ZN+1kTZUreOkxAvSoGh+HDVf4Ujs3ZRwYmoSSls0WkIi08QjbIoUwa User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:4613 Date: 2004-10-03T19:11:53+02:00 List-Id: Rick Santa-Cruz wrote: > Hi, > > I know what to understand about Dynamic Dispatching, but with the > following example in Ada I have some problems to understand the whole > thing: Unlike C++ dipaching in Ada is not connected to using pointers. It is connected to using Object'Class. > -- package Objects > package Objects is > type Object is abstract tagged private; > type Object_Ptr is access all Object'Class; Avoid "access all" unless you need it. The compiler need to do more checking when using "access all". > > procedure Draw(O: Object) is abstract; > > procedure Set_Text(O: in out Object; Text: String); > function Get_Text(O: Object) return String; > > private > type Object is abstract tagged record > Text: String(1..50); Consider using a Bounded_String or Unbounded_String here. > end record; > end Objects; > > package body Objects is > procedure Set_Text(O: in out Object; Text: string) is > begin > O.Text(1..Text'Length) := Text; This will leave you with garbage characters inside the string. Use Ada.Strings.Fixed_Strings which pad the string with spaces. > end Set_Text; > > function Get_Text(O: Object) return String is > begin > return O.Text; > end Get_Text; > end Objects; > > -- package Circles: > with Objects; > use Objects; > > package Circles is > type Circle is new Object with private; > > procedure Draw(C: Circle); > > package Constructor is > function Create(Text: String; Radius: Integer) return Circle; > end Constructor ; > > private > type Circle is new Object with record > Radius: Integer; > end record; > end Circles; > > with Ada.Text_IO; > use Ada.Text_IO; > > package body Circles is > procedure Draw(C: Circle) is > begin > Put_Line("Circle: " & Get_Text(C) & Integer'Image(C.Radius)); > end Draw; > > package body Constructor is > function Create(Text: String; Radius: Integer) return Circle is > C: Circle; > begin > Set_Text(C, Text); > C.Radius := Radius; > return C; > end Create; > end Constructor ; > end Circles; > > -- main-procedure: > with Ada.Text_IO; > with Objects; > with Circles; > > procedure Main is > procedure Call_Dynamic(Object_Ptr: Objects.Object_Ptr) is procedure Call_Dynamic(Object: Objects.Object'Class) is > begin > Objects.Draw(Object_Ptr.all); Objects.Draw(Object); > end Call_Dynamic; > C2 : Circles.Circle; C3 : Object_Ptr := new Circles.Circle'Class'(C2); > begin > C2 := Circles.Constructor.Create("Circle1",50); > Call_Dynamic (new Circles.Circle'(C2)); Call_Dynamic (C3); > end Main; > > My question is now, why with Call_Dynamic I call the Draw-procedure in > the > package Circles, although in the body of Call_Dynamic I directly call the > Method in the Objects Package? Surely it is syntactly correct, cause there > is an abstract mehtod Draw in the Objects-Package, but I don't understand > why such work with classes in different packages. I tested the same, when > all classes are in the same package and surely it worked too, and for me > and the understanding of dynamic-dispatching this is clear. How does this > internally work with the different-package approach as described above in > the source-code? "new Circles.Circle" created an object of Circles.Circle and not of Circles.Circle'Class. With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com