home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!hri.com!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: Preventing inheritance
- Message-ID: <5513@miramon.lulea.trab.se>
- Date: 11 Jan 93 15:21:16 GMT
- References: <1iqtnpINN283@news.cerf.net>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 25
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Howard Ferguson (hlf@nic.cerf.net) wrote:
-
- : 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. I could overload new
- : and check the size of the object that is being allocated, but there
- : are two catchs. The first minor one is that this is a run-time check,
- : and it would be far preferable to catch this sort of thing at compile
- : time. More seriously, they could over-ride the new operator and by-pass
- : the check.
-
- If your objects are always heap-based, you can prevent inheritance by
- making the destructor private:
-
- class X { public:
- static X* create() { return new X(); }
- static void destroy(X* it) { delete it; }
- private:
- X() { /*...*/ }
- ~X() { /*...*/ }
- };
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-