comp.lang.ada
 help / color / mirror / Atom feed
From: "Rick Santa-Cruz" <rick_santa_cruz75@msn.com>
Subject: Dynamich Dispatching...
Date: Sat, 2 Oct 2004 17:39:15 +0200
Date: 2004-10-02T17:39:15+02:00	[thread overview]
Message-ID: <cjmi3n$iat$03$1@news.t-online.com> (raw)

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:

-- package Objects
package Objects is
 type Object is abstract tagged private;
 type Object_Ptr is access all Object'Class;

 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);
 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;
 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
 begin
    Objects.Draw(Object_Ptr.all);
 end Call_Dynamic;
 C2: Circles.Circle;
begin
  C2 := Circles.Constructor.Create("Circle1",50);
 Call_Dynamic(new Circles.Circle'(C2));
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?

Thanks in advance,
Rick 





             reply	other threads:[~2004-10-02 15:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-02 15:39 Rick Santa-Cruz [this message]
2004-10-03 17:11 ` Dynamich Dispatching Martin Krischik
2004-10-03 22:59   ` Brian May
2004-10-04  8:22     ` Martin Krischik
2004-10-03 18:56 ` Ludovic Brenta
2004-10-04  4:45   ` Jeffrey Carter
2004-10-04 21:01     ` Ludovic Brenta
2004-10-05  0:32       ` Jeffrey Carter
2004-10-04  8:02 ` Dmitry A. Kazakov
2004-10-04 11:02   ` Brian May
2004-10-04 12:50     ` Dmitry A. Kazakov
2004-10-15 18:27       ` Matthew Heaney
2004-10-16 19:25         ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox