home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15975 < prev    next >
Encoding:
Text File  |  1992-11-09  |  5.3 KB  |  111 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: <1992Nov09.162957.31736@Cookie.secapl.com>
  6. Date: Mon, 09 Nov 1992 16:29:57 GMT
  7. References: <BwwvMw.DDC@slipknot.rain.com> <1992Nov05.165658.34729@Cookie.secapl.com> <Bx9vsr.4ML@slipknot.rain.com>
  8. Organization: Security APL, Inc.
  9. Lines: 100
  10.  
  11. In article <Bx9vsr.4ML@slipknot.rain.com> robert@slipknot.rain.com.UUCP (Robert Reed) writes:
  12. >In article <1992Nov05.165658.34729@Cookie.secapl.com> frank@Cookie.secapl.com (Frank Adams) writes:
  13. >|In article <BwwvMw.DDC@slipknot.rain.com> robert@slipknot.rain.com.UUCP (Robert Reed) writes:
  14. >|>I have not violated the intended semantics, I have generalized them, within
  15. >|>reasonable confines of the language.
  16. >|
  17. >|Since I'm giving the example, I know what the intended semantics are.  If I
  18. >|am defining the Rectangle class in a context where I can make assertions, I
  19. >|will assert that setWidth does not change the height, and vice versa.  This
  20. >|is the intended semantics, even though the assertions are not available in
  21. >|the language.
  22. >
  23. >  If C++ had declarative
  24. >assertions, I would expect that placing an assertion on the width and height of
  25. >a Square should not affect its base Rectangle class.
  26.  
  27. That isn't the constraint I was referring to.  I would put a constraint on
  28. the setWidth function in the Rectangle class that it not change the height.
  29. *This* is the assumption that will be violated.
  30.  
  31. >|void setDimensions(Rectangle& r, int x, int y) {
  32. >|    r.setWidth(x);
  33. >|    r.setHeight(y);  // corrected -- originally read setHeight
  34. >|}
  35. >|
  36. >|I expect this function to work: after it is called, r.width() should be x,
  37. >|and r.height() should be y.
  38. >
  39. >(I believe the 2nd setWidth() should be a setHeight().)   I see nothing in my
  40. >counter-proposal which would deny the operation of this function.
  41.  
  42. But I can do the following:
  43.  
  44.     Square s;
  45.     setDimensions(s, 2, 3);
  46.  
  47. This does not work; after the call, the width will not be 2.  (Of course,
  48. presented this baldly, I would not expect it to work; but I could be passing
  49. s to some other function which took a rectangle argument, and called
  50. setDimensions on it; the resulting bug is quite subtle.)
  51.  
  52. >|>|>  Alternatively, you could make Rectangle a subclass of
  53. >|>|>Square, define a setPrimaryDimension(int) for Square and add
  54. >|>|>setSecondaryDimension(int) for the specialized Rectangle class.
  55. >|
  56. >|There is another problem with this, if I decide to generalize a bit: I may
  57. >|want to define:
  58. >|
  59. >|    template<class WidthT, class HeightT>
  60. >|    class Rectangle {...}
  61. >|
  62. >|    template<class SideT>
  63. >|    class Square : public Rectangle<SideT, SideT> {...}
  64. >|
  65. >|In this case, Rectangle cannot inherit from Square; it would have to change
  66. >|the types of the instance variables.  This is not an altogether artificial
  67. >|example; I worked on a word processor (not written in an O.O. language) in
  68. >|which horizontal and vertical distances were expressed in different units.
  69. >|(This program had no use for squares, however.)
  70. >
  71. >If your intent is to use Squares on a system with different horizontal and
  72. >vertical dimensions, then even your scheme of Const Inheritance doesn't solve
  73. >the problem.  You still have to deal with how to specify a square.  Is it
  74. >expressed in horizontal or vertical units?  Or both?  If you're going to
  75. >constrain the problem so as to exclude alternate solutions, make sure your own
  76. >solution still works.
  77.  
  78. Sigh.  Do I have to explain every little detail?  If I were simply using
  79. different dimensions for width and height, I wouldn't have bothered making a
  80. template.  The point of the template is that I may be using different types
  81. for the dimensions in different places.  For example, I might be using
  82. character widths and heights in one place, and points in another.  I would
  83. not be able to use Square in the first context, but it would be quite usable
  84. in the other.
  85.  
  86. Even more importantly, this is the kind of problem you run into when you
  87. violate the "isA" relationship in you class hierarchy -- the resulting
  88. program is fragile.  One of the chief advantages of OO programming is that
  89. later enhancements can be done easily.  This kind of fragility makes that
  90. much more problematic.
  91.  
  92. >My claim is still that this a brutish approach to a problem that has been
  93. >solved in many other languages, such as Modula-3, Eiffel, etc.; that is,
  94. >selective inheritance.  As such, const inheritance is a partial feature.  In
  95. >the example we have been abusing, all you really want to restrict is CERTAIN
  96. >geometry modifying functions--not even all of them.  For example, shouldn't I
  97. >be able to define a Rectangle::setScale() function which could be inherited by
  98. >Square?  It would change variables but not affect the geometric constraints
  99. >you've expressed concern about.  How about a Rectangle::setBoundaryColor()
  100. >function?  This is a nongeometric function and so should be equally usable by
  101. >Rectangles and Squares.
  102. >
  103. >My problem with your proposal is not that it would not provide some useful
  104. >extention to current syntax.  My concern is that your solution only covers a
  105. >small subset of the problem domain, and as such must be considered a partial
  106. >feature, a potential source of more frustration than facility.
  107.  
  108. Selective inheritance might be a useful feature to add to the language.  But
  109. I see const inheritance as a completion of a feature already in the
  110. language, not as an entirely new feature.
  111.