comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Tardieu <sam@ada.eu.org>
To: kbrun@ibm.net
Subject: Re: access types and dispatching subprograms
Date: 1996/06/25
Date: 1996-06-25T00:00:00+00:00	[thread overview]
Message-ID: <qw64to0cmbv.fsf@gargantua.enst.fr> (raw)
In-Reply-To: 4qjqre$44lo@news-s01.ny.us.ibm.net


>>>>> "Klaus" == kbrun  <kbrun@ibm.net> writes:

Klaus> I have some trouble with Ada95 dispatching subprograms and
Klaus> access types. AFAIK, a proc. call should dispatch if all
Klaus> corresponding actual parameters are of (*or pointing to*) type
Klaus> T'Class and if the formal parameters are of (*or pointing to*)
Klaus> type T. Still, the following code sample doesn't work.

It seems you haven't well understood how dispatching works. The
"compiler error" (in fact "invalid parameter list in call") seems
correct, because there is no procedure "disp" whose formals are of
type TC_p.

Dispatching works on subprograms whose first parameter is of a tagged
type (or access to a tagged type) (in fact all the parameters of the
same hierarchy of tagged types).

What you probably want to do is:

package T is

   type T is tagged null record;
   procedure Disp (P : access T);

   type T1 is new T with null record;
   procedure Disp (P : access T1);

   type TC_p is access all T'Class;

   procedure Do_Dispatch;

end T;

with Ada.Text_IO; use Ada.Text_IO;
package body T is

   procedure Disp (P : access T) is
   begin
      Put_Line ("access T");
   end Disp;

   procedure Disp (P : access T1) is
   begin
      Put_Line ("access T1");
   end Disp;

   procedure Do_Dispatch is
      ClassP : constant TC_p := new T1;
   begin
      Disp (ClassP);
   end Do_Dispatch;

end T;

with T; 
procedure Test is
begin
   T.Do_Dispatch;
end Test;

Execution gives:

% ./test
access T1

which is what you want.

   Sam

PS/ Note the use of access parameters (and not parameters of an access type)
-- 
  Samuel Tardieu -- sam@ada.eu.org




      parent reply	other threads:[~1996-06-25  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-23  0:00 access types and dispatching subprograms kbrun
1996-06-23  0:00 ` Robert A Duff
1996-06-23  0:00 ` Robert Dewar
1996-06-25  0:00 ` Samuel Tardieu [this message]
replies disabled

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