home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11608 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  3.1 KB

  1. Xref: sparky comp.lang.c++:11608 comp.std.c++:963
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!ftpbox!motsrd!news
  4. From: shang@corp.mot.com (David (Lujun) Shang)
  5. Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
  6. Message-ID: <1992Jul27.145018.7599@cadsun.corp.mot.com>
  7. Sender: news@cadsun.corp.mot.com
  8. Reply-To: shang@corp.mot.com
  9. Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
  10. References: <1992Jul25.172045.24675@ucc.su.OZ.AU>
  11. Date: Mon, 27 Jul 92 14:50:18 GMT
  12. Lines: 88
  13.  
  14. In article <1992Jul25.172045.24675@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU  
  15. (John MAX Skaller) writes:
  16. >     OK. My design is flawed. Why? Because I didnt write the
  17. > spec properly.
  18. >     Spec1: All animals can have a go at mating.
  19. >     If they are of one species (and opposite sex) then
  20. >     the female is flagged as pregnant.
  21. >
  22. Your spec. is still wrong. The proper spec. should be:
  23.  
  24.      Spec.: All animals of the same species can have a go at mating.
  25.  
  26. > This spec REQUIRES the interface:
  27. >     void Animal::mate(Animal&);
  28. >
  29.  
  30. This spec REQUIRES the interface:
  31.  
  32.         void Animal::mate(thiscalss&);
  33.  
  34. > because the spec says 'All animals can have a go at mating'.
  35. >
  36. because the spec should prohibit possible mismatch as much as possible.
  37.  
  38. > To check if the animals are of the same type, I provide
  39. > type information:
  40. >     virtual char* Animal::Species()=0;
  41. >
  42. Here you provide your own RTTI! We can always do that when we need RTTI.
  43.  
  44. > and the sex:
  45. >     enum {male,female,infertile} sex;
  46. > Now I can write:
  47. >     void Animal::mate(Animal& x)
  48. >     {
  49. >         if(sex==infertile || x.sex==infertile)return;
  50. >         if(strcmp(Species(),x.Species())) return;
  51. >         if(sex==x.sex)return;
  52. >         if(sex==female) pregnant=TRUE;
  53. >         else x.mate(*this);
  54. >     }
  55. >
  56. If we can provide a presice specification, why should we leave so many troubles  
  57. to the implementation?! If the implementor happened to forget to check the  
  58. species, what is the result?! And in most cases, we can check the species at  
  59. compile-time, why should we leave it to run-time?
  60.  
  61. What I asked above just illustrates the difference of your own RTTI and the  
  62. language supported RTTI. The latter is much better and safer.
  63.  
  64. > NOW: I want to demonstrate why using RTTI is WRONG in this case,
  65. > and why the Species() function is required.    
  66. >
  67. Remenber, your function Species() IS JUST FOR THE PURPOSE TO PROVIDE RTTI. 
  68. > The demonstration is so simple, and applies so generally, I wonder
  69. > why I didnt think of it before.
  70. >     class DOG : Animal { ....
  71. >     class HUND : Animal { .. // german programmer
  72. > See: BOTH classes are IMPLEMENTATIONS of the very same animal
  73. > species. But their exact types are different.
  74. > So they could never mate. Tell that to German Shepard
  75. > and an Alsatian :-)
  76. This is never an execuse. If it is, your Species() function has the same  
  77. problem:
  78.  
  79.      char* DOG::Species()
  80.         { return "DOG"; }
  81.      char* DOG::Species()
  82.         { return "HOUND"; }   // german programmer
  83.  
  84. I'm rather confused: first, you provide your own RTTI function for this case,  
  85. then you try to prove that using RTTI is wrong. What on earth are your trying  
  86. to say?
  87.  
  88. David Shang
  89.  
  90.  
  91.