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,d56e8cd008b09d8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-07-22 10:52:54 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn1feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail Message-ID: <3B5B138A.3E38FBE@worldnet.att.net> From: James Rogers X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Construction/Destruction and copy semantics References: <9jeuvb$not$1@wanadoo.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Sun, 22 Jul 2001 17:52:53 GMT NNTP-Posting-Host: 12.86.33.137 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 995824373 12.86.33.137 (Sun, 22 Jul 2001 17:52:53 GMT) NNTP-Posting-Date: Sun, 22 Jul 2001 17:52:53 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:10423 Date: 2001-07-22T17:52:53+00:00 List-Id: Bertrand Augereau wrote: > > One of the (many) features I like in C++ is constructors/destructors > semantics. > I find it better than calling a construct procedure and destroy procedure > for non-trivial data types (those that have pointers to other ressources, or > sockets, or whetever, for instance). > You can't forget it this way. > Is the problem that I want to stick a C++ pattern in Ada? How do you Ada > programmers manage this problem? > Second thing, I tried to write some basic containers, and they rely on the > := 'operator'. It seems to me you can't overload it in Ada. > So if you have a type which needs lazy copying for instance, you have to > define a 'copy' procedure for this type. > And then your container relies on this procedure. It is not that generic any > more at this point. > It seems bad to me as you can't instantiate your generic containers with the > types of somebody else who uses 'clone' instead. > Is it right? How do you adress this problem? The Ada approach to both of these questions is to create your tagged types as extensions of either Ada.Finalization.Controlled or Ada.Finalization.Limited_Controlled for the first question, and Ada.Finalization.Controled for the second question. The Ada.Finalization package defines two abstract tagged types. Ada.Finalization.Controlled is for use when you want to create non-limited types. Ada.Finalization.Limited_Controlled is used when you want to create limited types. The Ada.Finalization.Controlled type has three primitive procedures: Initialze, Adjust, and Finalize. Initialize is similar to a C++ default constructor. Initialize is invoked immediately after the normal initialization of a controlled object. Finalize is invoked immediately before finalization of any of the components of a controlled object. Adjust is invoked as the last step of an assignment to a non-limited controlled object. The Ada.Finalization package is described in section 7.6 of the Ada Reference Manual. I am sure you have noticed that the Initialization procedure does not correspond to all forms of C++ constructors, but only the default constructor. Ada allows you to handle this problem in several ways. The manner which is IMHO most similar to a non-default C++ constructor is to provide the tagged type with a discriminant. This allows you to parameterize the creation of an object of that type. Combining type discriminants with the Initialize procedure will give you all the flexibility of a C++ constructor. The adjust procedure can be used to customize the assignment of a type. Note that Ada, by default, performs deep copies. Of course one reason for that is that Ada code generally uses fewer access types than C++ (compared with C++ pointers and references). Shallow copying only makes sense for access types. If you define an Ada record (tagged or not) containing fields which are instances of access types, then copying an object of the record will automatically result in shallow copies of the accessed values. The "deep" copy operation for an access type is to copy the value from the corresponding access type. Jim Rogers Colorado Springs, Colorado USA