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,741d9c75087d538f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-06 11:18:34 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mcq95@earthlink.net (Marc A. Criley) Newsgroups: comp.lang.ada Subject: Re: Cloning a Class Wide Data Type with "new"? Date: 6 Jun 2003 11:18:33 -0700 Organization: http://groups.google.com/ Message-ID: <254c16a.0306061018.38393868@posting.google.com> References: <3EE097D5.2010901@cogeco.ca> NNTP-Posting-Host: 12.158.183.115 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1054923514 373 127.0.0.1 (6 Jun 2003 18:18:34 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 6 Jun 2003 18:18:34 GMT Xref: archiver1.google.com comp.lang.ada:38767 Date: 2003-06-06T18:18:34+00:00 List-Id: "Warren W. Gay VE3WWG" wrote in message news:<3EE097D5.2010901@cogeco.ca>... > I need to be able to allocate and then copy an arbitrary > tagged type (based upon a base type) into the newly > allocated object. There's gotta be a way to do this.. > I just can't remember it! > > The problem is the operation of the form of : > > Cloned : Base_Access := new Base_Class'(Orig); Here's what I did to get it to compile... Cloned : Base_Access := new Base_Class'Class'(Base_Class(Orig)); ...AND it seemed to work--though it'd be a little iffy for me to explain in detail what's going on. Marc A. Criley Modified sample program follows: with Ada.Text_IO; use Ada.Text_IO; procedure TProg is type Base_Type is tagged record V : Integer; end record; type Base_Access is access all Base_Type'Class; subtype Base_Class is Base_Type'Class; type Derived_Type is new Base_Type with record Z : Natural; end record; -- This is the original object to be cloned Orig : Derived_Type := ( V => 32, Z => 99 ); -- This is a local copy (easy) Copied : Base_Class := Orig; -- Allocate Cloned to be same as Orig (the problem) Cloned : Base_Access := new Base_Class'Class'(Base_Class(Orig)); begin Put_Line("Cloned.V = " & Cloned.V'Img); Put_Line("Cloned.Z = " & Derived_Type(Cloned.all).Z'Img); end TProg;