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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4651b0d82680c5d2 X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: Polymorphism/Inheritence Date: 1997/02/28 Message-ID: <3316F788.470A@gsfc.nasa.gov>#1/1 X-Deja-AN: 222087095 References: <5f0pcc$5b9@news.cps.udayton.edu> Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Reply-To: Stephen.Leake@gsfc.nasa.gov Newsgroups: comp.lang.ada Date: 1997-02-28T00:00:00+00:00 List-Id: In article <5f0pcc$5b9@news.cps.udayton.edu> abacon@cps.udayton.edu (Adam C. Bacon) writes: > [description of example; replaced with compilable code:] package Parent is type Vehicle is tagged private; type Vehicle_Pointer is access Vehicle'Class; private type Vehicle is tagged record Maker_ID : Integer; end record; end Parent; package Parent.Child is type Car is new Vehicle with private; procedure Create (Maker_ID : in Integer; Wheels : in Integer); private type Car is new Vehicle with record Wheels : Integer; end record; end Parent.Child; package body Parent.Child is Pointer : Vehicle_Pointer; procedure Create (Maker_ID : in Integer; Wheels : in Integer) is Temp_Car : Car := (Maker_ID, Wheels); begin Pointer := new Car; Pointer.all := Temp_Car; end Create; end Parent.Child; > > Pointer.all := Temp_Car; > > | > > dynamically tagged expression required Jon Anthony replies with an informative discussion of class wide types, and suggests: > > Pointer.all := Vehicle'Class(Temp_Car); > > Now, this compiles and runs OK on GNAT 3.09. But I don't know if it > is really correct... > This certainly works, and is consistent with the RM, as Jon points out. This is a view conversion; it does not change the value of Temp_Car as it copies it. Pointer.all is of type Vehicle'class; this is a different type than Car, so a view conversion is required. However, depending on what Create really does, there are better ways to do this. For example: procedure Create (Maker_ID : in Integer; Wheels : in Integer) is begin Pointer := new Car'(Maker_ID, Wheels); end Create; This avoids the entire issue, and saves a copy operation. If Create truly needs a Temp_Car for other reasons, then: procedure Create (Maker_ID : in Integer; Wheels : in Integer) is Temp_Car : Car'class; -- error begin Pointer := new Car; ... mess with Pointer and/or Temp_Car ... Pointer.all := Temp_Car; end Create; But class wide objects need initialization, so we try: procedure Create (Maker_ID : in Integer; Wheels : in Integer) is Temp_Car : Car'class := (Maker_ID, Wheels); begin Pointer := new Car; ... mess with Pointer and/or Temp_Car ... Pointer.all := Temp_Car; end Create; This seems like it should work, but now I get: Temp_Car : Car'class := (Maker_ID, Wheels); parent-child.adb:8:31: aggregate not available for limited array This is a very weird message, since there are neither arrays nor limited types present. I'll send it to gnat_report. What is this error trying to tell me? -- - Stephe