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,92a6dd552088ca30 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder.news-service.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Constructor for a class with a task defined Date: Thu, 01 Sep 2011 11:13:32 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="dFCm8HWntFqmDIilBLqEJQ"; logging-data="3613"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LesHmYtRijTkTY85GTgmuCA9L6ZZc5K8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (darwin) Cancel-Lock: sha1:lI624tkbstCzWNGS4DNYczrS6KQ= sha1:b50RLoUmnbbj6H1jzHk9AbLtem8= Xref: g2news1.google.com comp.lang.ada:20818 Date: 2011-09-01T11:13:32+01:00 List-Id: "Rego, P." writes: > I'm trying to implement a Constructor method for a class which have a > task type pointed to the class, so > > in spec: > type Small_Class; > > task type My_Task_Type (This_Small_Class : access Small_Class); > > type Small_Class is tagged limited > record > My_Task : My_Task_Type (Small_Class'Access); > end record; > > and in body: > > function Construct return access Small_Class is > begin > return new Small_Class'(My_Task => ???????); > end Construct; > > > So, how can I do it? You don't need to do anything. The code below works (I realise now that I've changed the names, sorry about that): package Small_Class is type Instance is limited private; type Instance_P is access all Instance; function Create (Index : Integer) return Instance_P; procedure Start (This : Instance_P); private task type T (This : not null access Instance) is entry Start; end T; type Instance is limited record The_T : T (This => Instance'Access); Index : Integer := 0; end record; end Small_Class; with Ada.Text_IO; use Ada.Text_IO; package body Small_Class is task body T is begin accept Start; Put_Line ("T (" & Integer'Image (This.Index) & " ) started."); end T; function Create (Index : Integer) return Instance_P is Result : Instance_P := new Instance; begin Result.Index := Index; return Result; end Create; procedure Start (This : Instance_P) is begin This.The_T.Start; end Start; end Small_Class; with Small_Class; with Ada.Text_IO; use Ada.Text_IO; procedure Small_Class_Test is H : Small_Class.Instance_P; begin H := Small_Class.Create (42); Put_Line ("waiting ..."); delay 1.0; Put_Line ("starting ..."); Small_Class.Start (H); end Small_Class_Test; As others have suggested, you could try compiling using GNAT GPL 2011 and -gnat12 and changing Create to function Create (Index : Integer) return Instance_P is begin return new Instance'(Index => Index, others => <>); end Create; but when I tried it I got a bug box (which I shall report directly!)