home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17947 < prev    next >
Encoding:
Text File  |  1992-12-14  |  8.0 KB  |  275 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rosevax!camax01!kotula
  3. From: kotula@camax.com (Jeff Kotula)
  4. Subject: Virtual base classes summary
  5. Message-ID: <1992Dec14.212948.15144@camax.com>
  6. Organization: CAMAX Systems, Inc.
  7. Date: Mon, 14 Dec 1992 21:29:48 GMT
  8. Lines: 265
  9.  
  10. Here is a summary of the responses to my post a few weeks ago about virtual
  11. base classes.  Good stuff!  Thanks for the responses...
  12.  
  13.  
  14. From pat@frumious  Thu Dec  3 06:52:22 1992
  15. Date:     Wed, 2 Dec 1992 18:45:13 -0500
  16. From: pat@frumious (Patrick Smith)
  17. To: kotula@camax.com
  18. Subject: Re: Virtual base classes
  19.  
  20. You wrote:
  21. |Me and this other guy (who shall remain nameless) have been trying to
  22. |figure out the appropriate use for virtual base classes.  Given the
  23. |following inheritance hierarchy:
  24. |        A
  25. |       / \
  26. |       B C
  27. |       | |
  28. |       D E
  29. |       \/
  30. |        F
  31. |
  32. |where we only want one copy of A, which set of inheritances should be
  33. |virtual?
  34. |
  35. |    1) A to B  and  A to C
  36.  
  37. This one.
  38.  
  39.  
  40.  
  41. |    2) D to F  and  E to F
  42.  
  43. This won't work; if you do (2) but not (1), then every D object will
  44. contain its own A subobject (not sharable with any other A object),
  45. and every E object will contain its own A subobject (also not sharable).
  46. So in an F you will have a D subobject (containing one A object)
  47. and an E subobject (containing a different A object).
  48.  
  49. After (2), if you then created a new class,
  50.  
  51.    class G : public F, public virtual D {};
  52.  
  53. then G would only contain one D subobject (because of virtual
  54. inheritance).
  55.  
  56. -- 
  57. Patrick Smith
  58. uunet.ca!frumious!pat
  59. pat%frumious.uucp@uunet.ca
  60.  
  61. From jem@vuse.vanderbilt.edu  Wed Dec  2 18:51:40 1992
  62. Date: Wed, 2 Dec 92 18:01:02 CST
  63. From: jem@vuse.vanderbilt.edu (John Eric Meyer)
  64. To: kotula@camax.com
  65. Subject: Re: Virtual base classes
  66. Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
  67. Cc: 
  68.  
  69.    A
  70.   / \
  71.  B   C
  72.  |   |
  73.  D   E
  74.   \ /
  75.    F
  76.  
  77. As I understand the nature of the beast, A to B and A to C must be virtual,
  78. while D to F and E to F need not be.  Declaring D to F and E to F as virtual
  79. *without* virtualizing A will not have the desired effect.  The virtual
  80. state of inherited objects applies only to the object inherited as virtual,
  81. not to the object and all its ancestry.
  82.  
  83.    A
  84.    |
  85.    B
  86.   / \
  87.  C   D
  88.   \ /
  89.    E
  90.  
  91. In this case 
  92.  
  93.    class B : public A;
  94.    class C : virtual public B;
  95.    class D : virtual public D;
  96.    class E : public C, public D;
  97.  
  98. would result in E containing only one copy of B and one copy of A (within B, 
  99. if you will).  A is in a sense virtualized by association. On the other hand
  100.  
  101.    class B : virtual public A;
  102.    class C : public B;
  103.    class D : public B;
  104.    class E : public C, public D;
  105.  
  106. would result in E containing two copies of B but just one copy of A.
  107.  
  108. Hope this helps ( moreover, I hope this it correct! )
  109.  
  110. Eric
  111. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  112. As I tell my friends, never listen to me when I'm wrong.
  113. ---------------------------------------------------------------------------
  114.  
  115. From landauer@Eng.Sun.COM  Thu Dec  3 06:52:28 1992
  116. Date: Wed, 2 Dec 92 20:24:29 PST
  117. From: landauer@Eng.Sun.COM (Doug Landauer)
  118. To: kotula@camax.com
  119. Subject: Re: Virtual base classes
  120. Organization: Sun Microsystems, Mt. View, Ca.
  121.  
  122. >       A
  123. >        / \
  124. >        B C
  125. >        | |
  126. >        D E
  127. >        \/
  128. >       F
  129. > where we only want one copy of A, which set of inheritances should be
  130. > virtual?
  131. >     1) A to B  and  A to C
  132. >        or
  133. >     2) D to F  and  E to F
  134. > I suspect that either will work, but which is best, and why?
  135.  
  136. No, only the #1 choice is appropriate.  One way to state the rule is to
  137. say that if you want a base class to be shared, its closest derived
  138. class must inherit it virtually.  Using the #2 choice, there will be
  139. two A's.
  140.  
  141. Another way to look at it is the cfront way:  think of the most likely
  142. implementation, and it will show you the semantics.  In this case,
  143. virtual inheritance means a pointer instead of inclusion.  To share a
  144. base class, we must have a pointer to *that* base class.  In your #2
  145. choice above, we have only pointers to D and to E -- we cannot share
  146. those.  We do *not* have any pointer to any A.
  147.  
  148. >  3) Are there hidden gotchas?
  149.  
  150. One gotcha is the one-way nature of virtual base class pointers.  For
  151. instance, in one common OOP idiom, you have a class which implements
  152. "list-of-ptr-to-base-class", and you populate that list with ptrs to
  153. various derived classes, and upon extraction from the list, you
  154. downcast the pointer to the type you happen to know it really is.
  155. This idiom works fine *only in the absence* of virtual inheritance.
  156. Because the pointer is one-way, you *cannot* cast a ptr-to-base into
  157. being a ptr-to-derived if the inheritance is virtual.
  158.  
  159. One other gotcha is in debugging:  if your debugger's C++ support is
  160. weak, you must understand this virtual-base-pointer business, else
  161. the bits in memory make no sense.  (Sun's dbx support is pretty
  162. reasonable in this respect.)
  163. -- 
  164. Doug Landauer -- landauer@eng.sun.com               _
  165. Sun Microsystems, Inc. -- STE, SunPro, IPE         La no ka 'oi.
  166.  
  167. From nikki@trmphrst.demon.co.uk  Tue Dec  8 14:57:49 1992
  168. Path: trmphrst.demon.co.uk!nikki
  169. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  170. Subject: Re: Virtual base classes
  171. Reply-To: nikki@trmphrst.demon.co.uk
  172. To: kotula@camax.com
  173. Date: Tue, 08 Dec 92 19:24:41  GMT
  174. Organization: Trumphurst Ltd.
  175.  
  176. In article <1992Dec2.163139.8326@camax.com> you write:
  177. > Me and this other guy (who shall remain nameless) have been trying to
  178. > figure out the appropriate use for virtual base classes.  Given the
  179. > following inheritance hierarchy:
  180. >     A
  181. >        / \
  182. >        B C
  183. >        | |
  184. >        D E
  185. >        \/
  186. >     F
  187. > where we only want one copy of A, which set of inheritances should be
  188. > virtual?
  189. >     1) A to B  and  A to C
  190. >                or
  191. >     2) D to F  and  E to F
  192. > I suspect that either will work, but which is best, and why?
  193. Only 1 will work.
  194.  
  195. > My understanding is that the semantics of diamond inheritance is an inherent
  196. > problem in OO techniques.  At the moment I'm more concerned about
  197. > pragmatics, though.  For instance:
  198. >     1) Are most implementations of virtual base classes reliable?
  199. No.
  200.  
  201. >     2) What compilers are not reliable in this respect?
  202. To my knowledge, Zortech C++ 3.0r4 and Borland C++ 3.0. I have heard that 
  203. at least one cfront based compiler also has problems.
  204.  
  205. -- 
  206. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  207. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  208.  
  209. From nikki@trmphrst.demon.co.uk  Thu Dec 10 12:58:16 1992
  210. Path: trmphrst.demon.co.uk!nikki
  211. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  212. Subject: Re: Virtual base classes
  213. Reply-To: nikki@trmphrst.demon.co.uk
  214. X-Mailer: cppnews $Revision: 1.30 $
  215. Date: Wed, 09 Dec 92 19:22:15  GMT
  216. Organization: Trumphurst Ltd.
  217.  
  218. In article <9212082108.AA25407@kotula.SGI> you write:
  219. > > >     1) Are most implementations of virtual base classes reliable?
  220. > > No.
  221. > Do you know if the errors are at compile-time or run-time?  What are
  222. > some of the manifestations?
  223. Borland gives errors at compile time for the following ...
  224.  
  225. class base {
  226. public:
  227.     virtual void abstract() = 0;
  228.     };
  229.  
  230. class d1 : virtual public base {
  231. public:
  232.     virtual void abstract() { }
  233.     };
  234.  
  235. class d2 : virtual public base {
  236.     };
  237.  
  238. class d3 : public d1, public d2 {
  239.     };
  240.  
  241. d3 Ad3;        // Borland thinks this is an abstract class. I don't.
  242.  
  243. Zortech gives runtime errors (i.e. sometimes crashes horribly) with 
  244. similar code (but which does some real work). This is caused by the thunk 
  245. which adjusts the offset to the this pointer getting the adjustment 
  246. wrong.
  247.  
  248. I don't know about cfront, I can't afford it :-)
  249.  
  250. -- 
  251. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  252. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  253.  
  254.  
  255.  
  256.  
  257. Markku Sakkinen also forwarded several interesting papers he's written on
  258. the general subjects of problems with multiple inheritance.
  259.  
  260.  
  261. -- 
  262. -------------------------------------------------------------------------------
  263. jeff                |   The doctors say there's a 50-50 chance that he'll
  264. kotula@camax.com    |   live...although there's only a 10 percent chance of
  265. Camax Systems, Inc. |    that.
  266.