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-Thread: 103376,adae40bcc4d01baf X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!out03b.usenetserver.com!news.usenetserver.com!in04.usenetserver.com!news.usenetserver.com!nx01.iad01.newshosting.com!newshosting.com!newspeer.monmouth.com!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Ada 95 constructors on limited types Date: Wed, 02 Jan 2008 11:02:03 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <08dc2b30-6c8c-4cff-9f2e-c0d4c377972d@i3g2000hsf.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1199289727 8097 192.74.137.71 (2 Jan 2008 16:02:07 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 2 Jan 2008 16:02:07 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:CDozum1emffLgyfsa9YY2LL3XXE= Xref: g2news1.google.com comp.lang.ada:19146 Date: 2008-01-02T11:02:03-05:00 List-Id: shaunpatterson@gmail.com writes: > function Create (Test_Value : Integer) return Class_Access is > begin > return new Class' (Value => Test_Value); > -- Works only with Ada 2005... I am stuck using Ada 95 > end Create; Right. Dmitry showed how to write it. > -- function Create (Test_Value : Integer) return Class is > -- begin > -- return Class' (Value => Test_Value); > -- end Create; > > end Parent.Child; > > > So, the first create works in Ada2005 with: > > Test_Pointer : Parent.Class_Access := Parent.Child.Create (5); > > I have also tried the second Create unsuccessfully with > > Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5); That's not the right syntax for an allocator. See RM-4.8(2), or any Ada textbook. (The allocator in the first Create above uses the right syntax for an initialized allocator; Dmitry showed an uninitialized allocator.) In Ada 2005, you can say: Test_Pointer : Parent.Class_Access := new Parent.Child.Class'(Parent.Child.Create (5)); Or if you say "use Parent, Parent.Child;": Test_Pointer : Parent.Class_Access := new Child.Class'(Create (5)); This is illegal in Ada 95. In fact, the second Create function won't work in Ada 95 even without the allocator. You can make it legal by making the type nonlimited. Limited types are rather limited (so to speak) in Ada 95, because you're not allowed to explicitly initialize objects (as the above allocator does). This annoying restriction is removed in Ada 2005. > I don't understand how to initialize limited types and their pointers. You can use default initialization. You can use a discriminant to pass information to the default. You can initialize nonlimited components of a limited object, as Dmitry showed. You can switch to nonlimited types. Or you can switch to Ada 2005. Here's an example with a discriminant: type Class (Initial_Value : Integer) is new Parent.Class with private; ... private type Class (Initial_Value : Integer) is new Parent.Class with record Value : Integer := Initial_Value; -- Or some expression -- like Func(Initial_Value)*2 -- would be legal, too. end record; Then you can say: ... := new Parent.Child.Class(Initial_Value => 5); and the heap object's Value component will be default-initialized to 5. > Any help? Again, my project currently is tied down to Ada95 (gnat > 3.16p) Sorry to hear that. 3.16p is quite old! - Bob