home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11673 < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.9 KB  |  50 lines

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