comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Construction/Destruction and copy semantics
Date: Sun, 22 Jul 2001 17:52:53 GMT
Date: 2001-07-22T17:52:53+00:00	[thread overview]
Message-ID: <3B5B138A.3E38FBE@worldnet.att.net> (raw)
In-Reply-To: 9jeuvb$not$1@wanadoo.fr

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



  parent reply	other threads:[~2001-07-22 17:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-22 16:33 Construction/Destruction and copy semantics Bertrand Augereau
2001-07-22 17:42 ` Ehud Lamm
2001-07-22 17:52 ` James Rogers [this message]
2001-07-22 18:16   ` Bertrand Augereau
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox