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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,56131a5c3acc678e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-26 09:16:12 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.com!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: Question about OO programming in Ada Date: Wed, 26 Nov 2003 17:36:48 +0100 Organization: AdaCL Message-ID: <3357244.u7mJpG4184@linux1.krischik.com> References: <21836716.smDW2dK3SM@linux1.krischik.com> 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 1069866904 07 14708 Xp5+V+Y1J7Ak2Yd 031126 17:15:04 X-Complaints-To: usenet-abuse@t-online.de X-ID: E2V6+ZZJoegV2PsVKMiYujzqooKyPbQgCw+Fu8y4wmbjPmeIQ5LmYY User-Agent: KNode/0.7.2 Xref: archiver1.google.com comp.lang.ada:2968 Date: 2003-11-26T17:36:48+01:00 List-Id: Ekkehard Morgenstern wrote: > > "Martin Krischik" schrieb im Newsbeitrag > news:21836716.smDW2dK3SM@linux1.krischik.com... >> Ekkehard Morgenstern wrote: >> > Do I have to use class-wide types for object-oriented programming, or >> > could I use regular access types? >> > >> > Like, when I declare a procedure >> > >> > procedure A ( B: in access all T'Class ) >> >> I made this mistake as well when I started. For OO it should be only: >> >> procedure A ( B: in out T ) >> >> Unlike C++ no access is needed in Ada. And a class wide Types make your >> procedure non "virtual". Some Tutorials are quite bad about describing > that >> point. > > Are you sure? > > Should it not read at least > > procedure A ( B: in out T'Class ) No. I thought so as well in the beginning. But it is the other way round. "virtual" aus standart in Ada - using T'Class makes it not "virtual" > Because when I inherit from a class I would like the methods of the base > class to work on the derived class as well. Yes. > Both the Ada Rationale and the Guide for C/C++ programmers say that you > have to declare the first parameter of a procedure or the return value of > a function to be of the class-wide type of the class to get the dynamic > runtime dispatching behaviour from Ada (like virtual functions in C++). When you call the function, not when you declare the funtion! procedure A ( B: in out T); I : T; I_Class : T'Class := T'Class (I); A (I_Class); > And how do I cast an object back to the reduced record type? And is that > necessary at all? Best you you rename them: I_Class : T'Class renames T'Class (I); Of better detail look at procedure Analyze_GNU in http://adacl.sourceforge.net/html/sarHTML-CommandLine__adb.htm to see how dispaching calls (This_C) und calls to parent methods (This_S) are done in Ada. > The Ada 95 Reference says that the "all" attribute provides read-write > access to the object, while omitting it would only provide read-only > access? Or did I get that wrong? no! access is access to heap storage. access all is access to heap, stack and other storage. access constant is constant access. > (btw, I made short cuts with my examples, I know not all of the notation I > gave can be actually compiled) No Problem. >> > procedure A ( B: in out T ) >> >> Much better then using an acces type. > > But how about inheritance? Will the procedure operate on a type derived > from T? Yes. >> > procedure A ( B: in T ) >> >> Is like using "const&" in C++. > > How about function returns? Can I return a reference to an object in the > same way? Again the Ada Compiler will use the most efficient way. >> In Ada access type are not as often needed as in C++. So don't use then >> unless you need them. References are done automatily by Ada. >> >> If you know C++ then you will know about: >> >> virtual void A (T& B); or virtual void A (T B); >> >> As I said Ada will make the '&' Reference automaticly. > > Does that help to avoid casting as well? And how do I return a reference > from a function? Or should I use procedures with in out parameters > instead? Depends. You can return any non limited type with a function. Ada will will use by reference or by value depends what is best. functions however can only have in parameters. And you can not return a limited type. Use out parameter if functions won't work. With Regards Martin -- mailto://krischik@users.sourceforge.net http://adacl.sourceforge.net