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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Brian Drummond Newsgroups: comp.lang.ada Subject: Re: Why is the destructor called multiple times after I declare an object? Date: Mon, 11 Jan 2016 17:02:01 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: <293c58ac-4ebd-488a-abcc-b6e88811eec8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 11 Jan 2016 17:02:01 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="da745e888d4a5182b5fda6212bbb0a63"; logging-data="14743"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194F7kcPrFfEI2TYQxII/Zi3wOzHyrdkR4=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:vrTAtaewwMFNwIhd1T1ds7SQOYc= Xref: news.eternal-september.org comp.lang.ada:29088 Date: 2016-01-11T17:02:01+00:00 List-Id: On Sun, 10 Jan 2016 19:35:43 -0800, Andrew Shvets wrote: > On Sunday, January 10, 2016 at 9:18:57 PM UTC-5, Jeffrey R. Carter > wrote: >> On 01/10/2016 06:37 PM, Andrew Shvets wrote: >> > >> > Why is "Resetting values of Creat to defaults." displaying 6 times as >> > opposed to just twice? >> >> ARM 7.6(13-17.1) apply here. Note that Init declares a local variable >> of the type, > > I see. Thank you for your explanation. Note that for the default initialisation case, if you override Initialize, it will be called automatically. overriding procedure Initialize( Creat : in out Creature) is begin Ada.Text_IO.Put_Line("Initializing values of Creat to defaults."); Creat.Name := Ada.Strings.Unbounded.To_Unbounded_String ("Ferret"); Creat.Legs := 3; Creat.WeightInGrams := 100; Creat.HeightInCm := 7; end Initialize; (yes the ferret lost a leg somehow). Because it's a procedure, with In Out parameters, it operates in-place, avoiding both the creation of a temporary object, and assignment. Now the main program Var1 : Animal.Creature; --:= Animal.Init; -- default constructor Var2 : Animal.Creature := Animal.Init("Elephant", 4, 4000000, 500); begin Animal.Print_Record(Var1); Animal.Print_Record(Var2); end main_animal; initialises both variables to the defaults, then operates as Jeffery answered for the non-default Init function (I overrode Adjust to track assignments). Initializing values of Creat to defaults. Initializing values of Creat to defaults. Adjusting Creature named Elephant Resetting values of Creat to defaults. Adjusting Creature named Elephant Resetting values of Creat to defaults. The animal: The name: Ferret Number of legs: 3 Weight in grams: 100 Height in cm: 7 The animal: The name: Elephant Number of legs: 4 Weight in grams: 4000000 Height in cm: 500 Resetting values of Creat to defaults. Resetting values of Creat to defaults. Rewriting Init (the non-default constructor) as a procedure would eliminate the intermediate variables and assignments. It would have to be called at the start of the main program instead of as an initialiser. Regrettably there seems to be no syntax for calling non- default initializers on a controlled type. -- Brian