comp.lang.ada
 help / color / mirror / Atom feed
From: Tucker Taft <stt@averstar.com>
Subject: Re: Constructors/Destructors in Ada95
Date: 2000/10/20
Date: 2000-10-20T20:10:47+00:00	[thread overview]
Message-ID: <39F0A6C7.E592AFFB@averstar.com> (raw)
In-Reply-To: 39EF5431.BF4CD793@bigfoot.com

Francois Godme wrote:
> ...
> 
> To Tom Moran, I will say that the problem in the Ada.Finalization package
> is the Initialize procedure.

It is a mistake to think that the Initialize procedure
represents Ada's "constructor" feature.  As you recognize
below, Initialize is the rough equivalent of the default
constructor.  For any other kind of constructor, you simply
write a function, or procedure with an [in] out parameter.
Ada also has a builtin constructor to simplify the
creation of these user-defined constructors, namely an aggregate
(array, record, or record extension).  This is available
for non-limited types, in places where the type is
visible (e.g. inside the package where it is declared).  

An aggregate constructor can be combined with dynamic
storage allocation, via an initialized allocator.

For (inherently) limited types, non-default construction must be 
done essentially one component at a time, even when inside
the package where the type is defined.  Of course, from outside
the package, an appropriate procedure would be called to
invoke the user-chosen construction operation, and that
procedure would take care of the appropriate component-by-
component actions.

> 
> The procedures Adjust and Finalize do not cause any problems. But the
> Initialize procedure does not respect the Ada style guide for the same
> reason as above. The Initialize procedure is inherited. And worst, besides
> the object itself to initialize, there are no other parameters passed to
> initialize the object. It is like if in C++, all classes were forced to
> have a default constructor and the instances were first initialized to
> useless values.

Initialize is exactly the part of the default constructor,
over and above the default initialization specified within
the type declaration itself.  If the type extension adds
more components that have default initialization, these
default initializations will occur, even if Initialize is
inherited.  In fact, I would generally recommend that
programmers minimize the use of Initialize, if they can
do it all with per-component default initialization.

But the key point is not to think of "Initialize" as
a general purpose constructor.  It is simply the default
"constructor" (and really only part of that, given the
per-component defaults that are applied first).
"Regular" functions and procedures are used to do
any other "construction."
> 
> That's why the Ada style guide goes on to state: "Consider splitting the
> initialization and construction of an object."

They should probably say consider splitting
*default* initialization and *explicit* construction.
This is only really *necessary* for limited types.
For non-limited types, using a function to initialize
the object upon declaration/dynamic allocation is
quite appropriate.

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Commercial Division, AverStar (formerly Intermetrics)
(http://www.averstar.com/services/IT_consulting.html)  Burlington, MA  USA




  parent reply	other threads:[~2000-10-20  0:00 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-10-18  0:00 Constructors/Destructors in Ada95 Francois Godme
2000-10-19  0:00 ` tmoran
2000-10-19  0:00   ` Francois Godme
2000-10-19  0:00     ` Ted Dennison
2000-10-20  0:00     ` Tucker Taft [this message]
2000-10-20  0:00       ` Francois Godme
2000-10-21  0:00         ` Marin David Condic
2000-10-23  0:00       ` Francois Godme
2000-10-24  0:00         ` Ray Blaak
2000-10-25  0:00           ` Francois Godme
2000-10-25  0:00           ` Marin David Condic
2000-10-25  0:00             ` dmitry6243
2000-10-25  0:00               ` Pascal Obry
2000-10-26  0:00                 ` dmitry6243
2000-10-25  0:00               ` mark.biggar
2000-10-26 11:44                 ` dmitry6243
2000-10-26 13:25                   ` Robert A Duff
2000-10-27  8:10                     ` dmitry6243
2000-10-26 17:55                   ` tmoran
2000-10-27  8:10                     ` dmitry6243
2000-10-26 21:31                 ` Tucker Taft
2000-10-27  8:46                   ` dmitry6243
2000-10-27  7:12             ` Ray Blaak
2000-10-27 18:11           ` Francois Godme
2000-10-30 11:36             ` Robert A Duff
2000-10-30 22:03               ` dale
2000-10-22  0:00     ` rwilson007007
2000-10-22  0:00       ` Francois Godme
2000-10-24  0:00         ` rwilson007007
2000-10-19  0:00 ` Marin David Condic
2000-10-19  0:00 ` Ted Dennison
  -- strict thread matches above, loose matches on Subject: below --
2000-10-29 22:51 rwilson007007
2000-10-30  4:03 ` Ray Blaak
2000-10-30 12:13 ` Marin David Condic
2000-10-30 16:39   ` Randy Brukardt
replies disabled

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