comp.lang.ada
 help / color / mirror / Atom feed
* accessing subprogram & inheritance problem
@ 2001-02-05 23:12 VC
  2001-02-06  4:43 ` tmoran
  2001-02-08  1:53 ` Robert Brantley
  0 siblings, 2 replies; 6+ messages in thread
From: VC @ 2001-02-05 23:12 UTC (permalink / raw)


How do I make the following program to compile and work?
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.
Is it possible at all with Ada95?

Vicky
---------------------------------------------------------------
with Ada.Text_Io;
use Ada.Text_Io;

procedure Mytest is

   type Root_T;

   type Action_Ptr_T is access
     procedure(Object : in Root_T);

   type Root_T is tagged
      record
         Action_Ptr : Action_Ptr_T;
      end record;

   procedure Root_Action (Object : in Root_T) is
   begin
      Put_Line("Root Action");
   end;

   type Child_T is new Root_T with null record;

   procedure Child_Action (Object : in Child_T) is
    begin
      Put_Line("Child Action");
   end;

   My_Object : Root_T;
   My_Child : Child_T;

begin

   My_Object.Action_Ptr := Root_Action'Access;
   My_Object.Action_Ptr(My_Object);  -- this printed out "Root Action"

   My_Child.Action_Ptr := Child_Action'Access;
   My_Child.Action_Ptr(My_Child);  -- would like it to print "Child Action"

end Mytest;






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: accessing subprogram & inheritance problem
  2001-02-05 23:12 accessing subprogram & inheritance problem VC
@ 2001-02-06  4:43 ` tmoran
  2001-02-06  7:59   ` Sven Nilsson
  2001-02-08  1:53 ` Robert Brantley
  1 sibling, 1 reply; 6+ messages in thread
From: tmoran @ 2001-02-06  4:43 UTC (permalink / raw)


>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?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: accessing subprogram & inheritance problem
  2001-02-06  4:43 ` tmoran
@ 2001-02-06  7:59   ` Sven Nilsson
  0 siblings, 0 replies; 6+ messages in thread
From: Sven Nilsson @ 2001-02-06  7:59 UTC (permalink / raw)


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;



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: accessing subprogram & inheritance problem
  2001-02-05 23:12 accessing subprogram & inheritance problem VC
  2001-02-06  4:43 ` tmoran
@ 2001-02-08  1:53 ` Robert Brantley
  2001-02-08  4:36   ` tmoran
  1 sibling, 1 reply; 6+ messages in thread
From: Robert Brantley @ 2001-02-08  1:53 UTC (permalink / raw)




VC wrote:

> How do I make the following program to compile and work?
> 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.
> Is it possible at all with Ada95?

Hi.
No not possible as far as I know, but I am far from an expert on Ada.
There are COM and Corba bindings available if that is what you are trying
to achieve. But it does make me curious.  How do you implement COM type
interfaces
in Ada?

Robert Brantley




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: accessing subprogram & inheritance problem
  2001-02-08  1:53 ` Robert Brantley
@ 2001-02-08  4:36   ` tmoran
  2001-02-08 18:48     ` Robert Brantley
  0 siblings, 1 reply; 6+ messages in thread
From: tmoran @ 2001-02-08  4:36 UTC (permalink / raw)


>How do you implement COM type interfaces in Ada?
 Roughly:
    type QueryInterface_Spec is access function (This : access VTable_Type;
      riid:Pointer_To_Constant_GUID;
      ppvObj:Pointer_To_Interface) return HResult;
    pragma Convention(StdCall,QueryInterface_Spec);
    type AddRef_Spec is access function (This : access VTable_Type)
    return UInt;
    pragma Convention(StdCall,AddRef_Spec);
    ...
    type VTable_Type is record
      QueryInterface : QueryInterface_Spec;
      AddRef : AddRef_Spec;
      ...
 Look at David Botton's GNATCOM at www.adapower.com for automatic tools,
explanation, etc.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: accessing subprogram & inheritance problem
  2001-02-08  4:36   ` tmoran
@ 2001-02-08 18:48     ` Robert Brantley
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Brantley @ 2001-02-08 18:48 UTC (permalink / raw)


Ahhhh Cool.

Thanks,

Robert brantley

tmoran@acm.org wrote:

> >How do you implement COM type interfaces in Ada?
>  Roughly:
>     type QueryInterface_Spec is access function (This : access VTable_Type;
>       riid:Pointer_To_Constant_GUID;
>       ppvObj:Pointer_To_Interface) return HResult;
>     pragma Convention(StdCall,QueryInterface_Spec);
>     type AddRef_Spec is access function (This : access VTable_Type)
>     return UInt;
>     pragma Convention(StdCall,AddRef_Spec);
>     ...
>     type VTable_Type is record
>       QueryInterface : QueryInterface_Spec;
>       AddRef : AddRef_Spec;
>       ...
>  Look at David Botton's GNATCOM at www.adapower.com for automatic tools,
> explanation, etc.




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-02-08 18:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-05 23:12 accessing subprogram & inheritance problem VC
2001-02-06  4:43 ` tmoran
2001-02-06  7:59   ` Sven Nilsson
2001-02-08  1:53 ` Robert Brantley
2001-02-08  4:36   ` tmoran
2001-02-08 18:48     ` Robert Brantley

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