comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Help - trying to understand dynamic dispatching
Date: 1999/04/09
Date: 1999-04-09T00:00:00+00:00	[thread overview]
Message-ID: <7elnj9$jjq@hobbes.crc.com> (raw)
In-Reply-To: 923610308.5473.0.nnrp-09.c2de848f@news.demon.co.uk


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.







  parent reply	other threads:[~1999-04-09  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-08  0:00 Help - trying to understand dynamic dispatching Steve Folly
1999-04-09  0:00 ` dennison
1999-04-09  0:00 ` Matthew Heaney
1999-04-09  0:00 ` David Botton
1999-04-09  0:00 ` David C. Hoos, Sr. [this message]
1999-04-11  0:00 ` Steve Folly
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox