comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: new/free of abstract types
Date: 31 Mar 2003 07:59:06 -0800
Date: 2003-03-31T15:59:06+00:00	[thread overview]
Message-ID: <1ec946d1.0303310759.5c6babbb@posting.google.com> (raw)
In-Reply-To: 3e87f119$1@news.wineasy.se

"Frank" <franjoe@frisurf.no> wrote in message news:<3e87f119$1@news.wineasy.se>...
> 
> I have an abstract type, and wish to perform a new/free of objects extended
> from this type, inside one of the methods/subprograms
> that has the abstract type as a classwide parameter (in one of the type
> methods, to say it in Java/C++ language).  See example below.
> 
> It seems that I can't to it this way:
> 
> See example below to see how "The_New_Object" is declared:
> 
> The_New_Object := new MyType;
> 
> because the real property of the type is not fully known in the scope.
> 
> What I wish is to create a new object of the same type as the object
> "P_Parameter" (see example below).
> Can it be done - if so, how is the syntax?
> 
> I haven't yet tried to do this with the "Freeing" method, so a tip about that
> would also be appreciated.
> 
> 
> Frank
> 
> example:
> ---------------
> type Pointer_To_MyType is access all MyType;
> 
> type A_Type is record
>    The_New_Object : Pointer_To_MyType;
> end record;
> 
> Something_Global : A_Type;
> 
> ---------------
>   procedure Subprogram (P_Parameter : out MyType'Class) is
>   begin
>       Something_Global.The_New_Object := new MyType; <---?

You have to allocate objects of T'Class, like this:

type Pointer_To_MyType_Class is access all MyType'Class;

procedure Op (Arg : in MyType'Class) is  --in param, not out
begin
   Something_Global.The_New_Object := new MyType'Class'(Arg);

The_New_Object will inherit the type tag of Arg.  This is sort of like
the prototype pattern, except that it's built into the language.

Freeing the object isn't a problem, because Ada.Unchecked_Deallocation
accepts an indefinte type (as T'Class), e.g.

procedure Free is
   new Ada.Unchecked_Deallocation 
     (MyType'Class, 
      Pointer_To_MyType_Class);

Now you can say:

Free (Something_Global.The_New_Object);

Let me know if any of this isn't clear.  You asked an important
question, and it touches upon aspects of the language with which many
programmers aren't familiar.



  reply	other threads:[~2003-03-31 15:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-31  7:41 new/free of abstract types Frank
2003-03-31 15:59 ` Matthew Heaney [this message]
2003-04-01  8:30   ` Frank
2003-04-01 15:13     ` Matthew Heaney
replies disabled

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