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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1e6b0935218e0d0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: private classes Date: Sun, 03 Oct 2004 18:36:21 +0200 Organization: AdaCL Message-ID: <7036140.cAlS8YMQ4m@linux1.krischik.com> References: Reply-To: krischik@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1096822493 02 14084 6OuPXOh1tBaiTvG 041003 16:54:53 X-Complaints-To: usenet-abuse@t-online.de X-ID: rYxQogZF8eOKNd0Qsn28ixREYgHWDG2l4l8S4hVY5ib0aFYaGl1QoU User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:4608 Date: 2004-10-03T18:36:21+02:00 List-Id: Rick Santa-Cruz wrote: > Hi, > > sorry for so many question, but always when I thought I understand it, I > read further and see a new problem... so given the following source-code: > package Classes is > type Base_1 is tagged private; > > type Derived_1 is new Base_1 with private; > > procedure Proc(B: Base_1); > > private > type Base_1 is tagged record > Number: Integer; > end record; > > type Derived_1 is new Base_1 with null record; > end Classes; > > package body Classes is > procedure Proc(b: Base_1) is > begin > null; > end Proc; > end Classes; > > with Classes; > > procedure main is > D: Classes.Derived_1; D_Class : Classes.Base_1'Base renames Classes.Base_1'Base (D); > begin > Classes.Proc(D); Classes.Proc(D_Class); > end Main; > > I get an error in calling Classes.Proc(D). Although I thought that cause > Derived_1 inherits from Base_1 the call should be possible. In fact > exactly this I found in the book from John English in chapter 14.5. Why > can't I compile such, although I thought the function Proc is visible for > clients of the package Classes. It's the strong typing in Ada. For a C++ programmer used to using pointers and references which convert itself this might come as a suprise. Here you should remember that "this" is indeed a pointer. > My second question is then, if the above does not work Well it does work with the fix I have made. You should note that Ada does not automaticly slice objects. This has advantages and disadvantages. I show you the advantage: type Base_1_Access is access Base_1; D_Pointer = new Base_1'(D_Class); In C++ the operator new would slice D down to Base_1 - that is all the Derived_1 information will be lost and only the copy constructor of Base_1 is called. In Ada it isn't. Ada will copy the complete object and use the Adjust from Derived_1. And that allows for the equivalent of "vector " to be as usefull as "vector ". {Ada.Containers.Vector will be added in Ada 2005 - Test versions are available on the net.} As a price you pay for this quite handy feature you need to use Base_1'Base whenever you don't know what actual class an object could be. With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com