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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,765548fafec25b6c X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Help - trying to understand dynamic dispatching Date: 1999/04/09 Message-ID: <7elnj9$jjq@hobbes.crc.com>#1/1 X-Deja-AN: 464567705 References: <923610308.5473.0.nnrp-09.c2de848f@news.demon.co.uk> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: Coleman Research Corporation Newsgroups: comp.lang.ada Date: 1999-04-09T00:00:00+00:00 List-Id: Steve Folly wrote in message <923610308.5473.0.nnrp-09.c2de848f@news.demon.co.uk>... >Help! I've been trying to understand how dynamic dispatching works in Ada95, >particularly the syntax required - ie. when and where you need the class >wide types. > >I'm not going to explain my problem here - my problem really is "how do you >do it?". >I'm more familiar with the C++ method of virtual functions. > >Unless someone wants to explain and perhaps give a concrete example in C++ >and it's equivalent Ada95 :-), a helpful push in the direction of a book, >paper or website describing such a beast would be most appreciated. > There is two small examples which includes dispatching on my FTP site: ftp://ftp.ada95.com/pub/pet_store.tgz and ftp://ftp.ada95.com/pub/tagged_types.tgz which illustrate the equivalent of C++ virtual functions. In pets.ads, the function Image_Of is declared as abstract, which is like C++'s pure virtual function. For any type derived from Pets.Pet_Type, a concrete implementation must be defined. The Ada compiler enforces it. See, for example the file pets-mammals-dogs.adb for one concrete implementation. In the tagged_types package, the file messages.ads has the two following declarations: function Image_Of (Item : Message) return String is abstract; function Image (Item : Message'Class) return String; The first is, again, an abstract function for which a concrete implementation must exist for every type derived from Messages.Message. The second, is a class-wide function which may be called for any type derived from Messages.Message, even for derived types not yet existing at the time the function Image is declared. The code and the commentary in the source files should make it clear what's going on. These examples also illustrate more than just the polymorphic aspects of Ada95. I hope you find them helpful. The last time I suggested someone look at these he found a bug. Maybe you will, too! If you have access to Ada95 for C and C++ Programmers by Samuel Johnston (ISBN 0-201-40363-3), there is a section starting on page 181, entitled "6.3.3 Polymorphism and 'Class (virtual functions)" with additional commentary.