home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: Virtual base classes
- Message-ID: <9235216.12447@mulga.cs.mu.OZ.AU>
- Keywords: virtual, class
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <1992Dec2.163139.8326@camax.com> <1992Dec3.202705.16646@ucc.su.OZ.AU> <rmartin.723768537@thor>
- Date: Thu, 17 Dec 1992 05:47:47 GMT
- Lines: 53
-
- rmartin@thor.Rational.COM (Bob Martin) writes:
-
- >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- >
- >| As a rule of thumb, all public inheritance should be made virtual,
- >| private inheritance non-virtual. (See papers by Markku Sakkinen
- >| for a full explanation of why)
- >
- >I am in some disagreement with the above statement. Virtual
- >inheritance is a necessary evil IMHO. It hobbles the language in
- >several important ways. It prevents valid downcasting. (No flames
- >please. I know downcasting is suboptimal, but prior to covariant return
- >types it is sometimes necessary). Also, VI adds some minor
- >inefficiencies to virtual dispatch. The worst aspect of VI, and the
- >one which I suspect makes you want to declare all public inheritance
- >as virtual, is that it must be used long before you know you need it.
- >i.e. In the diagram above, it is B and C which must virtually inherit
- >from A, but it is not until you have recognized the need for F that
- >you know that virtual inheritance is needed.
- >
- >This is really quite sick. Proper design of a project can alleviate
- >this a bit since the design will alert you to the need before you have
- >written any of the classes. But the problem can still occur later if
- >you extend the project by creating new classes.
- >
- >[...] I don't think it is acceptable to proliferate the
- >disadvantages of virtual inheritance by making all public inheritance
- >virtual.
-
- You have listed three disadvantages.
-
- 1. VI prevents downcasting.
-
- Well, unless you're willing to brave the flames, we can't really
- discuss this one much ;-) Suffice it to say that I for one don't
- consider this a disadvantage.
-
- 2. VI must used long before you know you need it.
-
- On the contrary, it is NON-virtual inheritance that causes this
- problem. If you use the "all public inheritance should be virtual"
- rule, then this problem does not occur.
-
- 3. VI is less efficient.
- True. But I wouldn't say that this "hobbles the language".
- The efficiency cost is necessary if classes are to be truly
- open.
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-