home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12684 < prev    next >
Encoding:
Text File  |  1992-08-20  |  3.9 KB  |  94 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: run-time type checking)
  5. Message-ID: <1992Aug21.000359.12509@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: <1992Aug15.182304.17396@ucc.su.OZ.AU> <4846@holden.lulea.trab.se>
  10. Date: Fri, 21 Aug 1992 00:03:59 GMT
  11. Lines: 81
  12.  
  13. In article <4846@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. >
  16. >****    From Meyer (p21) [Explicit interfaces]:
  17. >
  18. >    "Whenever two modules A and B communicate, this must be
  19. >     obvious from the text of A or B or both".
  20. >
  21. >    You take this to mean "...obvious from the interface of
  22. >    A or B", which is not what the author said, but perhaps what
  23. >    he meant.  Fine.
  24.  
  25.     Actually, this distinction is important. Note eiffel does not
  26. have separate interfaces. And in C++ a subroutine can USE a type
  27. not mentioned in its interface.
  28. >    
  29. >: A module is closed if it is available for use (finished, compiled,
  30. >: stored in a library). A module is open if it can be extended
  31. >: (new fields added, new functions defined, old functions restricted)
  32. >: The open/closed principle states a module should be both open and
  33. >: closed. The mechanism of inheritance and virtual functions, that is,
  34. >: having classes as modules, satifies these criteria.
  35. >
  36. >****    Right.
  37. >
  38. >    I would like to argue that your initial suggestion means that you
  39. >    will have to go back and extend the interface of a base class,
  40. >    to support operations in a new derived class.  
  41.  
  42.     If you want to use derived classes outside their design goal
  43. of implementing 'isA' relations.
  44.  
  45. >    If this is
  46. >    indeed an option for you, why don't you instead add the new
  47. >    operations as virtual functions of the base class?  The base
  48. >    class interface was after all not really closed, was it?
  49.  
  50.     That would be equivalent more or less (perhaps more work
  51.     to add more member names as virtuals).
  52. >
  53. >    Even Eiffel has "reverse assignments" to do safe
  54. >    downcasting (kind of supports interface openness).
  55. >
  56. >:     By making the ability to downcast Explicit in the interface
  57. >: of the base class, the closure of the base now depends on the listed
  58. >: derived classes too, so it is then open (excepting the named derived classes).
  59. >
  60. >****    I agree that this is a good thing, _if you can do it_. Base
  61. >    classes that will be extended by other programmers cannot
  62. >    in general list all future derived classes.
  63.  
  64.     Why would they have to list ANY? When you derive from a base
  65. class you should stick to the interface of the base class if you
  66. are going to use the derived class AS A base. If you are just
  67. using inheritance as a convenient form of composition, you should
  68. recognize that fact, and not expect polymorphic behaviour
  69. from unrelated types.
  70.  
  71. >
  72. >:     Another method is to use tagged pointers. ALL these methods
  73. >: suffer from lack of extensibility that downcasting doesn't. Thats
  74. >: because there is only ONE method of extensibility that is sound,
  75. >: and we already have it. Polymorphism. Virtual functions.
  76. >
  77. >****    Sound or not, when polymorphism does not suffice, you
  78. >    have to rely on something else, right?
  79. >
  80.     Yes. But we need a solution that does not compromise
  81. polymorphism. Adding new interfaces to a class is composition.
  82. Restricting the base class is polymorphism. They are 
  83. diametrically opposed mechanisms that can both be done in C++
  84. by inheritance. Unfortunately perhaps they can both be
  85. done simultaneously and legitimately, but I think it is necessary
  86. to separate the two conceptually. Allowing unconstrained
  87. downcasting might fail to do that.
  88.  
  89. -- 
  90. ;----------------------------------------------------------------------
  91.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  92.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  93. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  94.