comp.lang.ada
 help / color / mirror / Atom feed
* How to pass around access types to interfaces?
@ 2008-10-24 11:45 Sven
  2008-10-24 12:24 ` Niklas Holsti
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Sven @ 2008-10-24 11:45 UTC (permalink / raw)


I'm learning Ada and I've run into a big problem with passing around
access types to interfaces. I guess I just don't know all the
subtleties of access types, I'm used to C/C++ pointers. I didn't find
anything helpful on the net or in the  Ada Reference Manual.

I'm trying to get this code to run:

in some package:
  type My_Interface is interface;
  type My_Interface_Access is access all My_Interface'Class;

  procedure Do_Something( Item : in out Some_Object; With :
My_Interface_Access );

in main program:
  type Concrete_Type is new My_Interface with ...;


I need to pass an object of type Concrete_Type to the With parameter
of the Do_Something procedure. But the Compiler won't let me, no
matter what I do.


   My_Object : access Concrete_Type := new Concrete_Type;
   Do_Something( Item, My_Interface_Access( My_Object ) );

gives the compiler error "cannot convert local pointer to non-local
access type".

   My_Object : My_Interface_Access := new Concrete_Type;

gives the compiler error "type in allocator has deeper level than
designated class-wide type".

I found a way to pass my paramter by using this function:

  function Convert( Item : not null access Concrete_Type ) return
My_Interface_Access is
  begin
    return My_Interface_Access( Item );
  end;

But I don't this is such a good idea.

What is the right way to do this? I can't imagine that what I'm trying
to do is not supported in Ada.



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

end of thread, other threads:[~2008-10-31 14:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-24 11:45 How to pass around access types to interfaces? Sven
2008-10-24 12:24 ` Niklas Holsti
2008-10-24 12:49   ` Sven
2008-10-24 13:55     ` Ludovic Brenta
2008-10-24 21:32 ` sjw
2008-10-25  8:54   ` Sven
2008-10-25 15:43 ` Martin Krischik
2008-10-25 22:15   ` Robert A Duff
2008-10-26  8:02     ` Dmitry A. Kazakov
2008-10-26 15:50     ` Martin Krischik
2008-10-28 13:41       ` Maciej Sobczak
2008-10-29 14:15         ` Martin Krischik
2008-10-31 13:38           ` Maciej Sobczak
2008-10-31 14:46             ` Dmitry A. Kazakov
2008-10-29 12:42   ` Sven
2008-10-29 13:17     ` Dmitry A. Kazakov

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