comp.lang.ada
 help / color / mirror / Atom feed
From: Sven Nilsson <emwsvni@emw.ericsson.se>
Subject: Re: accessing subprogram & inheritance problem
Date: Tue, 06 Feb 2001 08:59:42 +0100
Date: 2001-02-06T08:59:42+01:00	[thread overview]
Message-ID: <3A7FAEEE.8B49DDA3@emw.ericsson.se> (raw)
In-Reply-To: xhLf6.58717$R5.3026117@news1.frmt1.sfba.home.com

tmoran@acm.org wrote:
> 
> >I would like to have a subprogram pointer in the root class which
> >can be used to point to the subprograms in the child class as well.
>   As a general rule, if Ada makes it hard to do something one way,
> it's a good bet there are problems that way, and there is a better
> approach.
>   In this case, why not just use dispatching, or even just overloading,
> to call the right ..._Action routine, rather than futz about with
> a pointer?

Exactly! This seems to be a very common misstake made by newcomers to
Ada. Pretty much everytime we have a new programmer working with us, he
or she will eventually come with this exact question. The answer is
always: "Don't do that". I don't really know if you CAN do it, but I do
know you shouldn't.
Instead overloading is the way to go. You should always call the child
first, which in turn may call the more general implementation of the
parent. If you design your code properly and think OO all the way, this
shouldn't be a problem. Your exampel (that's Vicky's example) is a bit
confusing. I looks pretty much like C in Ada-syntax. That's not good.
You should be doing something like this:

with Text_Io;

procedure Mytest is
   
   package Root is
      
      type Root_T is tagged
	 record
	    -- Whatever!
	    null;
	 end record;
      
      procedure Do_Action(Object : in Root_T);
   end Root;

   package body Root is

      procedure Do_Action(Object : in Root_T) is
      begin
	 text_io.put_line("Root Action");
      end Do_Action;
   end Root;
   
   use Root;

   package Child is
      
      type Child_T is new Root_T with null record;
      
      procedure Do_Action(Object : in Child_T);
   end Child;
   
   package body Child is
      
      procedure Do_Action(Object : Child_T) is
      begin
	 Text_Io.Put_Line("Calling parent:");
	 Do_Action(Root_T(Object));
	 Text_Io.Put_Line("Back again!");
	 text_io.put_line("Child Action");
      end Do_Action;
      
   end Child;
   
   use Child;

   Root_Object : Root_T;
   Child_Object : Child_T;
   
   begin
      Do_Action(Root_Object);
      Do_Action(Child_Object);
end Mytest;



  reply	other threads:[~2001-02-06  7:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-05 23:12 accessing subprogram & inheritance problem VC
2001-02-06  4:43 ` tmoran
2001-02-06  7:59   ` Sven Nilsson [this message]
2001-02-08  1:53 ` Robert Brantley
2001-02-08  4:36   ` tmoran
2001-02-08 18:48     ` Robert Brantley
replies disabled

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