home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17991 < prev    next >
Encoding:
Text File  |  1992-12-15  |  4.8 KB  |  137 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: Virtual base classes
  5. Message-ID: <1992Dec15.154004.8984@ucc.su.OZ.AU>
  6. Keywords: virtual, class
  7. Sender: news@ucc.su.OZ.AU
  8. Nntp-Posting-Host: extro.ucc.su.oz.au
  9. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  10. References: <1992Dec2.163139.8326@camax.com> <1992Dec3.202705.16646@ucc.su.OZ.AU> <rmartin.723768537@thor>
  11. Date: Tue, 15 Dec 1992 15:40:04 GMT
  12. Lines: 123
  13.  
  14. rmartin@thor.Rational.COM (Bob Martin) writes:
  15. >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  16. >|kotula@camax.com (Jeff Kotula) writes:
  17. >|>
  18. >|>        A
  19. >|>       / \
  20. >|>       B C
  21. >|>       | |
  22. >|>       D E
  23. >|>       \ /
  24. >|>      F
  25. >|>
  26. >|>where we only want one copy of A, which set of inheritances should be
  27. >|>virtual?
  28. >|>
  29. >
  30. >|    All of them.
  31. >
  32. >|    As a rule of thumb, all public inheritance should be made virtual,
  33. >|    private inheritance non-virtual. (See papers by Markku Sakkinen
  34. >|    for a full explanation of why)
  35. >
  36. >
  37. >(I'd like to see these papers.  Can you post a reference?)
  38.  
  39.     The author is sakkinen@jytko.jyu.fi, and regularly
  40. contibutes to comp.std.c++.
  41.  
  42. >
  43. >I am in some disagreement with the above statement.  Virtual
  44. >inheritance is a necessary evil IMHO.  It hobbles the language in
  45. >several important ways.  It prevents valid downcasting.  (No flames
  46. >please.  I know downcasting is suboptimal, but prior to covariant return
  47. >types it is sometimes necessary).  
  48.  
  49.     OK, I wont argue :-)
  50.  
  51. >Also, VI adds some minor
  52. >inefficiencies to virtual dispatch.  The worst aspect of VI, and the
  53. >one which I suspect makes you want to declare all public inheritance
  54. >as virtual, is that it must be used long before you know you need it.
  55. >i.e. In the diagram above, it is B and C which must virtually inherit
  56. >from A, but it is not until you have recognized the need for F that
  57. >you know that virtual inheritance is needed.  
  58.  
  59.     Apart from problems with efficiency and downcasting,
  60. the rationale is that ALL 'isA' relations, and especially
  61. abstractions, *must* be declared virtual anyhow. The reason
  62. is that such base classes should not ever be replicated,
  63. they must always be shared, and virtual inheritance is the
  64. only way to ensure this.
  65.  
  66.     If you do not do this, you break an important
  67. reusability principle, namely that the bases remain open.
  68.  
  69. >
  70. >This is really quite sick.  Proper design of a project can alleviate
  71. >this a bit since the design will alert you to the need before you have
  72. >written any of the classes.  But the problem can still occur later if
  73. >you extend the project by creating new classes.
  74.  
  75.     Therefore, always use virtual with public. Then,
  76. after the project is running but a bit slow and big, you can
  77. remove some of the unneccesary virtuals if you really have to.
  78. But you can only do this at the point of *closing*, when you
  79. will never extend or reuse the classes.
  80. >
  81. >I wish there were a way to declare F in such a way that you could say
  82. >that A should be a virtual base.  
  83.  
  84.     The only time you need non-virtual is when you require
  85. replicated inheritance. Most of them are cases where the derivation
  86. should also be private, because you are not implementing an 'isA'
  87. relation but just doing an implementation.
  88.  
  89.     But there are cases where you DO want
  90. to allow:
  91.  
  92.     A        A
  93.     /\        /\
  94.     B C        D E
  95.     \/        \/
  96.     F        G
  97.     \        /
  98.           H
  99.  
  100. and you cant do this at all.
  101.  
  102. >
  103. >Despite this, I don't think it is acceptable to proliferate the
  104. >disadvantages of virtual inheritance by making all public inheritance
  105. >virtual.
  106. >
  107. >BTW.  I think private inheritance is a very weak construct that should
  108. >be avoided altogether.  There is nothing that can be done with private
  109. >inheritance, that cannot be done, just as simply, with containment.
  110. >
  111.     Yes there is: you cant override virtuals with
  112. containment.
  113.  
  114.     Your view tends to indicate inheritance should principally
  115. be used for subclassing (isA). In that case it is correct
  116. to make all inheritance virtual. The corresponding inefficiency
  117. is then a consequence of the open/closed principle: the
  118. virtual pointers are required if the classes are to be genuinely
  119. open for arbitrary extension.
  120.  
  121.     They can be removed only be closing off a class and preventing
  122. its use as a base. This might be done with a private nested class,
  123. in which case you could sensibly omit 'virtual' to gain
  124. efficiency.
  125.  
  126.     In practice, I omit virtual most of the time,
  127. but then, I control all my classes, and can modify the lot
  128. and recompile all my programs if I need to: they dont need to
  129. be open and closed in the same sense as library classes
  130. published for all to use.
  131.  
  132. -- 
  133. ;----------------------------------------------------------------------
  134.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  135.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  136. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  137.