From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,704b8962c968e4b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-03-31 07:59:06 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: new/free of abstract types Date: 31 Mar 2003 07:59:06 -0800 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0303310759.5c6babbb@posting.google.com> References: <3e87f119$1@news.wineasy.se> NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1049126346 8771 127.0.0.1 (31 Mar 2003 15:59:06 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 31 Mar 2003 15:59:06 GMT Xref: archiver1.google.com comp.lang.ada:35827 Date: 2003-03-31T15:59:06+00:00 List-Id: "Frank" 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.