comp.lang.ada
 help / color / mirror / Atom feed
From: Sven <sven.weidauer@gmail.com>
Subject: How to pass around access types to interfaces?
Date: Fri, 24 Oct 2008 04:45:21 -0700 (PDT)
Date: 2008-10-24T04:45:21-07:00	[thread overview]
Message-ID: <c5772601-5fc5-4f2c-a75d-241accf9072d@k30g2000hse.googlegroups.com> (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.



             reply	other threads:[~2008-10-24 11:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-24 11:45 Sven [this message]
2008-10-24 12:24 ` How to pass around access types to interfaces? 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
replies disabled

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