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, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,901038687c38f61c X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!stamper.news.atl.earthlink.net!newsread3.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Idiom for a class and an object in Ada References: From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 20 Oct 2004 07:24:31 GMT NNTP-Posting-Host: 64.185.133.124 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.atl.earthlink.net 1098257071 64.185.133.124 (Wed, 20 Oct 2004 00:24:31 PDT) NNTP-Posting-Date: Wed, 20 Oct 2004 00:24:31 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:5495 Date: 2004-10-20T07:24:31+00:00 List-Id: Simon Wright writes: > I'm not so hung up as Matt about people using the word class (in > conversation; I agree about use in Ada code). But I have to say, > what's wrong with Matt's suggestion? seems to answer all your needs as > stated so far .. For reasons I can't fathom, many Ada95 developers still have a very Ada83 mindset. This is the canonical idiom in C++ for controlling instance creation: class C { public: static C* make( /* ... */ ); static void free(C*); void f(); //whatever private: C(); C(const C&); ~C(); C& operator=(const C&); }; Here, the ctor (and dtor) is declared as private, so the only way to make a C object is by calling factory function C::make(). This has a direct translation into Ada95: package P is type T (<>) is limited private; procedure Op (O : in out T); type T_Access is access all T; function New_T (...) return T_Access; private type T is limited record ...; end P; Here the package provides a factory function to dynamically create instances (or possibly return a pointer to statically declared objects, a la the Flyweight Pattern). Functions in Ada95 return constant objects. (In Ada83, functions returned values.) Types whose full view is limited are passed by reference. These two facts mean you can write the equivalent of a const reference in C++: const C& f(); by using a function whose return type is limited: package P is type T (<>) is limited private; procedure Op (O : in T); function Object1 return T; function Object2 return T; private type T is limited record ...; end P; This is exactly how Text_IO works. The objects returned by functions Standard_Input, Standard_Output, etc, correspond to the functions Object1 and Object2 above. This is the canonical Ada95 idiom for controlling instance creation, and for declaring well-known objects.