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,fcb020a71edc13e4 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-01-08 07:16:28 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!tar-atanamir.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: Problem whith objects, access and abstract tagged private types Date: Thu, 08 Jan 2004 16:22:47 +0100 Message-ID: References: <3ffd619b$0$22318$626a54ce@news.free.fr> NNTP-Posting-Host: tar-atanamir.cbb-automation.de (212.79.194.116) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1073574987 8285514 212.79.194.116 ([77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:4210 Date: 2004-01-08T16:22:47+01:00 List-Id: On Thu, 8 Jan 2004 14:58:44 +0100, "Bruno" wrote: >Hello to all, > >I'm newbie in Ada95 and I have some difficulties with the language. >I'll give you my specifications : > >Specif (.ads) : >--------------- > > package Objets is > > type Item_Type is abstract tagged private; > -- Action > procedure Traitement (F : in Item_Type); > -- Allows init > procedure Modifier (F : in out Item_Type'Class; > Nb : in Natural); > > type Item_Elmt is private; > function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt; > function To_Item_Type (F : in Item_Elmt) return Item_Type'Class; > > private > > type Item_Type is abstract tagged > record > Compteur : Natural := 0; > end record; > type Item_Elmt is access all Item_Type'Class; > > end Objets; > >I chose private types for hiding implementation of my types and access >types. > >My problem is that I can't make the function "To_Item_Elmt" that create >"Item_Elmt". > >I try to write this body : > > function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is > My_Tag : constant Ada.Tags.Tag := F'Tag; > begin > return new My_Tag' (F); > end To_Item_Elmt; > >The compilation indicates the following errors : > subtype mark required in this context > found "My_Tag" declared at line 30 > >If somebody could help and explain me how to resolve my problem, thanks for >his help. You cannot dispatch on bare tags. 1. Assuming that To_Item_Elmt indeed has to create a *copy* of the argument, then it could be: function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is begin return new Item_Type'Class'(F); end To_Item_Elmt; But it is a sort of suspicious design to hide the implementation of a pointer type, not making the pointer "smart". Observe that the following will leak: declare X : Derived_From_Item_Type; Y : Item_Elmt := To_Item_Elmt (X); begin null; end; -- A dangling copy of X produced by To_Item_Elmt is still there 2. If To_Item_Elmt should not copy the argument but just get an access to it, then it is impossible to do, because the argument is "in". Although, to complete the picture, there is so-called Rosen trick to circumvent that. [I would not recommed it for a novice] It goes as follows: type Item_Type is abstract tagged limited private; ... private type Item_Elmt is access all Item_Type'Class; type Item_Type is abstract tagged limited record Self : Item_Elmt := Item_Type'Unchecked_Access; Compteur : Natural := 0; end record; function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is begin return F.Self; end To_Item_Elmt; 3. However, probably, you need no Item_Elmt type at all, as a guess. -- Regards, Dmitry A. Kazakov www.dmitry-kazakov.de