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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,2f84446733b06aca X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,2f84446733b06aca X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: Converting C++ class to Ada Date: 1996/12/11 Message-ID: <32AF3A52.1340@watson.ibm.com>#1/1 X-Deja-AN: 203580104 references: <32ADF183.7195@lmtas.lmco.com> content-type: text/plain; charset=us-ascii organization: IBM Thomas J. Watson Research Center mime-version: 1.0 reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada,comp.lang.c++ x-mailer: Mozilla 3.0 (Win95; I) Date: 1996-12-11T00:00:00+00:00 List-Id: 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