home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15828 < prev    next >
Encoding:
Text File  |  1992-11-05  |  3.9 KB  |  88 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!secapl!Cookie!frank
  3. From: frank@Cookie.secapl.com (Frank Adams)
  4. Subject: Re: Const Inheritance
  5. Message-ID: <1992Nov05.165658.34729@Cookie.secapl.com>
  6. Date: Thu, 05 Nov 1992 16:56:58 GMT
  7. References: <BwuvIB.B23@slipknot.rain.com> <1992Oct29.173017.109790@Cookie.secapl.com> <BwwvMw.DDC@slipknot.rain.com>
  8. Organization: Security APL, Inc.
  9. Lines: 77
  10.  
  11. In article <BwwvMw.DDC@slipknot.rain.com> robert@slipknot.rain.com.UUCP (Robert Reed) writes:
  12. >In article <1992Oct29.173017.109790@Cookie.secapl.com> frank@Cookie.secapl.com (Frank Adams) writes:
  13. >| In article <BwuvIB.B23@slipknot.rain.com> robert@slipknot.rain.com.UUCP (Robert Reed) writes:
  14. >| >In article <1992Oct28.172457.98060@Cookie.secapl.com> frank@Cookie.secapl.com (Frank Adams) writes:
  15. >|>| Assume we have a class Shape, with a subclass Rectangle, and we want to add
  16. >|>| Square as a class.  It seems logical that Square should be a subclass of
  17. >|>| Rectangle.  However, if we make our shapes mutable objects, so that
  18. >|>| Rectangle might have a member function like setWidth(int), this does not
  19. >|>| work.  A Square cannot set its width without simultaneously setting its
  20. >|>| height.
  21. >|>
  22. >|>Not so.  You just have to ensure that the Square class has its own member
  23. >|>function setWidth(int) that constrains the height as well, and similarly with
  24. >|>any setHeight(int) function.
  25. >| 
  26. >| And what do I do with setDimensions(int,int) ?  Not to mention that you have
  27. >| violated the intended semantics of setWidth in the subclass; this is IMHO a
  28. >| mistake.
  29. >
  30. >I have not violated the intended semantics, I have generalized them, within
  31. >reasonable confines of the language.
  32.  
  33. Since I'm giving the example, I know what the intended semantics are.  If I
  34. am defining the Rectangle class in a context where I can make assertions, I
  35. will assert that setWidth does not change the height, and vice versa.  This
  36. is the intended semantics, even though the assertions are not available in
  37. the language.
  38.  
  39. >  Now, living within those confines may
  40. >require some redefinition of your planned interface, like the heretofore
  41. >unmentioned setDimensions(int, int).
  42.  
  43. The class definitions I gave were obviously incomplete; it was implicit that
  44. reasonable additional functions would be available.
  45.  
  46. In fact, I can define setDimensions as a non-member function completely
  47. outside the class definition:
  48.  
  49. void setDimensions(Rectangle& r, int x, int y) {
  50.     r.setWidth(x);
  51.     r.setWidth(y);
  52. }
  53.  
  54. I expect this function to work: after it is called, r.width() should be x,
  55. and r.height() should be y.
  56.  
  57. >|>  Alternatively, you could make Rectangle a subclass of
  58. >|>Square, define a setPrimaryDimension(int) for Square and add
  59. >|>setSecondaryDimension(int) for the specialized Rectangle class.
  60.  
  61. There is another problem with this, if I decide to generalize a bit: I may
  62. want to define:
  63.  
  64.     template<class WidthT, class HeightT>
  65.     class Rectangle {...}
  66.  
  67.     template<class SideT>
  68.     class Square : public Rectangle<SideT, SideT> {...}
  69.  
  70. In this case, Rectangle cannot inherit from Square; it would have to change
  71. the types of the instance variables.  This is not an altogether artificial
  72. example; I worked on a word processor (not written in an O.O. language) in
  73. which horizontal and vertical distances were expressed in different units.
  74. (This program had no use for squares, however.)
  75.  
  76. >| I don't mean to imply that this construct is necessary to create a usable
  77. >| class hierarchy; just that it seems to me to produce a cleaner
  78. >| specification thereof.
  79. >
  80. >That's not all I said in my last note.  My claim was that your language
  81. >enhancement is a partial feature which muddies the specification (by creating an
  82. >artificial restriction on inherited virtual functions to those that are declared
  83. >const), thus complicating the inheritance rules while providing no clear
  84. >advantage.
  85.  
  86. See my followup to my original article; it provides a point of view under
  87. which the restriction seems natural.
  88.