comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: Dynamich Dispatching...
Date: Sun, 03 Oct 2004 19:11:53 +0200
Date: 2004-10-03T19:11:53+02:00	[thread overview]
Message-ID: <1511723.0R0PesaRS9@linux1.krischik.com> (raw)
In-Reply-To: cjmi3n$iat$03$1@news.t-online.com

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




  reply	other threads:[~2004-10-03 17:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-02 15:39 Dynamich Dispatching Rick Santa-Cruz
2004-10-03 17:11 ` Martin Krischik [this message]
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