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

  1. Xref: sparky comp.lang.c++:11541 comp.std.c++:956
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  4. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  5. Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
  6. Message-ID: <1992Jul25.172045.24675@ucc.su.OZ.AU>
  7. Sender: news@ucc.su.OZ.AU
  8. Nntp-Posting-Host: extro.ucc.su.oz.au
  9. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  10. References: <1992Jul24.143359.3602@advtech.uswest.com> <1992Jul24.234628.21196@cadsun.corp.mot.com>
  11. Date: Sat, 25 Jul 1992 17:20:45 GMT
  12. Lines: 112
  13.  
  14. In article <1992Jul24.234628.21196@cadsun.corp.mot.com> shang@corp.mot.com writes:
  15. >> >(John MAX Skaller) writes: 
  16. >>We design our systems properly in the first place, and then the need
  17. >>to downcast never arises, when our thoughts wander in that 
  18. >>direction one can be sure we are not thinking virtuously,
  19. >>and perhaps the design itself is flawed.
  20. >> ....
  21. >
  22. >In article <1992Jul24.143359.3602@advtech.uswest.com> glw@io.uswest.com (Glenn  
  23. >Williams) replies:
  24. >>
  25. >> You have stated all this before, and every time you do, you fail to provide
  26. >> any examples of how you would design without downcasting. 
  27. >> 
  28. >> So, as I asked for earlier, let's have an example of a properly
  29. >> designed system.
  30. >> 
  31. >Indeed. Whenever I gave an example that demostrates the necessity of downcast,  
  32. >I was always considered to introduce a bad design, but I never get an example  
  33. >which is considered good and can solve the specific problem shown by my  
  34. >example. So, before blaming the "bad", show the good please.
  35.  
  36.     I am working on one. I did give a skeletal outline here
  37. on my reply bu the !@#$%^&*() (sorry) system logged me out,
  38. and this is the second attempt.
  39.  
  40. >
  41. >Here, I'd like to use the example given by John's own:
  42. >
  43. >> >(John MAX Skaller) writes: 
  44. >> >class Animal {
  45. >> >    int mate(Animal&);
  46. >> >};
  47. >> >class Dog : Animal {
  48. >> >    int mate (Animal&);
  49. >> >};
  50. >> 
  51. >> >We REALLY want Dog::mate(Dog&), but we can't have it.
  52. >>
  53. >
  54. >Therefore you need RTTI and downcast, 
  55.  
  56.     No, we dont. I stuffed up the design. (I can do it too :-)
  57.  
  58. >though a little bit too late to check the  
  59. >validation within the method body:
  60. >
  61. >class Dog : Animal
  62. >{     int mate (Animal& a)
  63. >      { if (typeof(a) != Dog) return an_error_code;
  64. >        ...
  65. >      };
  66. >}
  67.  
  68.     OK. My design is flawed. Why? Because I didnt write the
  69. spec properly.
  70.  
  71.     Spec1: All animals can have a go at mating.
  72.     If they are of one species (and opposite sex) then
  73.     the female is flagged as pregnant.
  74.  
  75. This spec REQUIRES the interface:
  76.  
  77.     void Animal::mate(Animal&);
  78.  
  79. because the spec says 'All animals can have a go at mating'.
  80.  
  81. To check if the animals are of the same type, I provide
  82. type information:
  83.  
  84.     virtual char* Animal::Species()=0;
  85.  
  86. and the sex:
  87.  
  88.     enum {male,female,infertile} sex;
  89.  
  90. Now I can write:
  91.  
  92.     void Animal::mate(Animal& x)
  93.     {
  94.         if(sex==infertile || x.sex==infertile)return;
  95.         if(strcmp(Species(),x.Species())) return;
  96.         if(sex==x.sex)return;
  97.         if(sex==female) pregnant=TRUE;
  98.         else x.mate(*this);
  99.     }
  100.  
  101. NOW: I want to demonstrate why using RTTI is WRONG in this case,
  102. and why the Species() function is required.    
  103.  
  104.  
  105. The demonstration is so simple, and applies so generally, I wonder
  106. why I didnt think of it before.
  107.  
  108.     class DOG : Animal { ....
  109.     class HUND : Animal { .. // german programmer
  110.  
  111. See: BOTH classes are IMPLEMENTATIONS of the very same animal
  112. species. But their exact types are different.
  113. So they could never mate. Tell that to German Shepard
  114. and an Alsatian :-)
  115.  
  116.  
  117. [Note: I answer the other bits of your post in my previous
  118. attempt butgot logged out, this time I will just have to
  119. pass. For the moment :-)
  120.  
  121. -- 
  122. ;----------------------------------------------------------------------
  123.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  124.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  125. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  126.