comp.lang.ada
 help / color / mirror / Atom feed
From: "Norman H. Cohen" <ncohen@watson.ibm.com>
Subject: Re: Converting C++ class to Ada
Date: 1996/12/11
Date: 1996-12-11T00:00:00+00:00	[thread overview]
Message-ID: <32AF3A52.1340@watson.ibm.com> (raw)
In-Reply-To: 32ADF183.7195@lmtas.lmco.com


Ken Garlington wrote:

>   (1) What do I do about the constructor? As I understand C++,
>       I need to force the user to supply values for the components of
>       Object_Type when something of that type is declared. How do I do
> this?

Stephen Leake mentioned one approach: discriminants.  Stephen is worried
about the fact that the discriminant values cannot change, but if you
are not too worried about space, you can have both a discriminant and an
ordinary record component for each piece of data that must be specified
upon creation of the object.  You can make your Object_Type controlled
and have its Initialize procedure copy the discriminant values into the
ordinary record components.  The only purpose of the discriminants is to
act, in effect, to parameterize the creation of an object.  (The use of
discriminants as C++-style "constructor parameters" is discussed on page
579 of Ada as a Second Language, where it is also stated that the role
of a constructor in C++ is most often played in Ada by a function
returning a new object.  Which brings us to a second approach...)

Another approach is to manipulate your objects outside the defining
package entirely in terms of references.  You provide a function that
takes the parameters needed for creating a new object and returns a
reference to the newly created object.  To ensure that Object_Type
objects cannot be created in any other way, make Object_Type both
limited and indefinite:

   type Object_Type (<>) is limited private;
   type Reference_Type is access Object_Type;
   function New_Object (...) return Reference_Type;

Outside of its defining package, Object_Type is indefinite, so an object
of the type can only be created if an initial value is specified for it
(in an object declaration or allocator), but Object_Type is also
limited, so no initial value can be specified for an object of this
type!  In other words, an Object_Type object cannot be declared or
allocated outside its defining package.  The corresponding full type
need not be either limited or indefinite, so inside the package body you
are free to allocate objects of the type and return the resulting access
values to the caller.  (Another example of this approach can be found on
pages 489-490 of Ada as a Second Language.)

-- 
Norman H. Cohen
mailto:ncohen@watson.ibm.com
http://www.research.ibm.com/people/n/ncohen




  parent reply	other threads:[~1996-12-11  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-12-10  0:00 Converting C++ class to Ada Ken Garlington
1996-12-11  0:00 ` Stephen Leake
1996-12-13  0:00   ` Stephen Leake
1996-12-11  0:00 ` Larry Kilgallen
1996-12-11  0:00 ` Norman H. Cohen [this message]
1996-12-12  0:00   ` Jon S Anthony
1996-12-11  0:00 ` Jon S Anthony
1996-12-12  0:00   ` Ken Garlington
1996-12-18  0:00   ` Jon S Anthony
1996-12-18  0:00     ` Matthew Heaney
1996-12-19  0:00     ` Robert A Duff
1996-12-20  0:00       ` Stephen Leake
1996-12-20  0:00         ` Robert A Duff
1996-12-20  0:00   ` Jon S Anthony
1996-12-20  0:00     ` Mitch Gart
1996-12-20  0:00   ` Norman H. Cohen
1996-12-21  0:00   ` Jon S Anthony
1996-12-12  0:00 ` Dale Stanbrough
1996-12-12  0:00   ` Constructors for Ada (was: Converting C++ class to Ada) Matthew Heaney
1996-12-13  0:00     ` Norman H. Cohen
1996-12-13  0:00       ` Matthew Heaney
1996-12-17  0:00   ` Robert I. Eachus
replies disabled

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