home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11673 comp.std.c++:974
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!sybus.sybus.com!myrddin!tct!chip
- From: chip@tct.com (Chip Salzenberg)
- Subject: Re: Downcasting and mortal sin (was run-time type checking)
- Message-ID: <2A75806A.826A@tct.com>
- Date: Tue, 28 Jul 1992 17:14:17 GMT
- References: <1992Jul23.154254.5306@ucc.su.OZ.AU> <1992Jul24.143359.3602@advtech.uswest.com> <RNH50ML@netmbx.netmbx.de>
- Organization: TC Telemanagement, Clearwater, FL
- Lines: 38
-
- According to jrobie@netmbx.netmbx.de (Jonathan Robie):
- >I'm confused. I have two animals, and I place them in a cage.
- >Who knows where these animals come from or what kind they are--
- >my classes may not have produced them.
-
- This is an important point: Further derivations of Animal may occur
- after all of "our" code is written and stashed in a library.
-
- >Now the program needs to decide if these animals should mate.
-
- No!
-
- The program may decide whether it _wants_ the animals to mate. But as
- for deciding the results of such an attempt, only the animals know!
- This fact is a consequence of the "who should know" principle: animals
- may make mating decisions based on factors that I can't imagine ahead
- of time.
-
- Therefore you need this:
-
- virtual boolean Animal::can_mate_with(const Animal&);
-
- Then there are can be any number of virtual member functions that each
- implementation of SomeAnimalType::can_mate_with() may use to determine
- if any given OtherAnimalType is suitable for mating. Some classes
- derived from Dog may be more picky than others, for example.
-
- Presto! No RTTI, and it still works.
-
- Remember the golden rule of software interfaces:
-
- Never try to predict success or failure of an operation.
- Instead, try it and see what happens.
-
- --
- Chip Salzenberg at Teltronics/TCT <chip@tct.com>, <73717.366@compuserve.com>
- "Do Rush place weird subliminal backmasked messages in their songs to
- compel unwilling geeks to commit evil .sig atrocities?" -- Dean Engelhardt
-