home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: Preventing inheritance
- Message-ID: <5517@miramon.lulea.trab.se>
- Date: 12 Jan 93 12:13:55 GMT
- References: <1993Jan11.195503.6255@athena.mit.edu>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 42
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Steve Berczuk (berczuk@space.mit.edu) wrote:
- : In article <1iqtnpINN283@news.cerf.net>, hlf@nic.cerf.net (Howard Ferguson) writes:
- : |>
- : |> If I create a class which I am supplying to other users, how do I
- : |> make sure that they do not inherit from it ever.
- [...]
- : |> yours totally paranoid,
- : Umm, why would you want to do this?
-
- You may wish to create a class completely without virtual functions
- (size or C compatibility considerations). If someone unwittingly
- inherits from such a class, the non-virtual destructor will not
- properly destroy data members added in the subclass. This may be
- harmless, lose a small amount of dynamic memory, or completely
- stop an application due to lack of resources such as file handles
- (if the subclass adds a data member that represents an open file).
-
- : [...]I can understand not wanting to "support" other users who inherit
- : the class, but that might be best approached by some sort of
- : documentation
-
- Not all users will notice this (they should, but they won't).
- As the supplier of the base class you may be indirectly affected
- when those users send in bug reports, or claim that your code
- is "unreliable". It is better to make sure that the compiler
- catches those errors early. Instead of subclassing the users
- will have to use the class as a data member.
-
- : (eg comments in the header to the effect of "this class
- : supports some wierd protocols that subclasses need to
- : enforce, subclass at your own risk").
-
- The protocol need not be "weird" in order to have the constraint.
- The size of small classes can double in size if you support
- subclassing (you should have at least a virtual destructor). You
- may not always want to pay that price.
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-