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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!darwin.sura.net!mips!mips!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: mating dogs and cats (Re: run-time type checking)
  5. Message-ID: <1992Jul29.173149.821@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1992Jul22.022218.1115@cadsun.corp.mot.com> <1992Jul23.154254.5306@ucc.su.OZ.AU> <TMB.92Jul28121957@arolla.idiap.ch>
  10. Date: Wed, 29 Jul 1992 17:31:49 GMT
  11. Lines: 62
  12.  
  13. In article <TMB.92Jul28121957@arolla.idiap.ch> tmb@idiap.ch writes:
  14. >In article <1992Jul23.154254.5306@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. >
  16. >   The problem is why you proposed 'thisclass' and is a real problem.
  17. >   It is a problem for which RTTI is the wrong solution,
  18. >   but never-the-less is the best we will get for a while.
  19. >   We really need multiple dispatch.
  20. >
  21. >   class Animal {
  22. >       int mate(Animal&);
  23. >   };
  24. >   class Dog : Animal {
  25. >       int mate (Animal&);
  26. >   };
  27. >
  28. >   We REALLY want Dog::mate(Dog&), but we cant have it.
  29. >
  30. >I don't see the problem.
  31. >
  32. >class Animal {
  33. >    virtual int mate(Animal &) = 0;
  34. >    virtual int mate_dog(Animal &other) {error("not a dog");}
  35. >    virtual int mate_cat(Animal &other) {error("not a cat");}
  36. >};
  37.  
  38.     You clearly DONT see the problem. You do not understand the
  39. open/closed principle. The problem with the definition of Animal
  40. above is that it is not OPEN. you cant make the mechanism work
  41. if you add RAT without modifying Animal. That is a violation
  42. of the open/closed principle. 
  43.  
  44. A base class must be CLOSED
  45. so that it can be compiled and the inteface frozen, even the
  46. source code thrown away for the definitions. Algorithms
  47. defined for the base class can be written, and compiled,
  48. and type checked WITHOUT knowledge of the derived classes.
  49. The closure of the base should  guarrantee that.
  50. (Which is why I am suspicious of downcasting).
  51.  
  52. A base class must be OPEN to extension by derivation.
  53. Arbitrary extension. AND in particular 'arbitrary'
  54. means not only that you can add any new derived classes
  55. you choose, but also that you can OMIT any you choose.
  56. Downasting voids that guarrantee.
  57.  
  58. Virtual functions achieve this. They are closed in the base.
  59. They are still open to overriding in the derived class.
  60.  
  61. Unconstrained Downcasting voids both the open-ness and the closedness.
  62. Without the open closed principle assured by the language,
  63. polymorphism is flawed and the system is unsound.
  64.  
  65. Indeed CASTING of any kind (changing the type of something)
  66. is unsound. Upcasting is sound because a derived
  67. 'isA' base, so you are not changing the type,
  68. just forgetting some details.
  69.  
  70. -- 
  71. ;----------------------------------------------------------------------
  72.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  73.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  74. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  75.