home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18949 < prev    next >
Encoding:
Text File  |  1993-01-11  |  1.4 KB  |  37 lines

  1. Path: sparky!uunet!noc.near.net!hri.com!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Preventing inheritance
  5. Message-ID: <5513@miramon.lulea.trab.se>
  6. Date: 11 Jan 93 15:21:16 GMT
  7. References: <1iqtnpINN283@news.cerf.net>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 25
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Howard Ferguson (hlf@nic.cerf.net) wrote:
  13.  
  14. : If I create a class which I am supplying to other users, how do I
  15. : make sure that they do not inherit from it ever. I could overload new
  16. : and check the size of the object that is being allocated, but there
  17. : are two catchs. The first minor one is that this is a run-time check,
  18. : and it would be far preferable to catch this sort of thing at compile
  19. : time. More seriously, they could over-ride the new operator and by-pass
  20. : the check.
  21.  
  22. If your objects are always heap-based, you can prevent inheritance by
  23. making the destructor private:
  24.  
  25. class X { public:
  26.     static X* create() { return new X(); }
  27.     static void destroy(X* it) { delete it; }
  28. private:
  29.     X() { /*...*/ }
  30.     ~X() { /*...*/ }
  31. };
  32. -- 
  33. --------------------------------------------------------------------------
  34. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  35. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  36. --------------------------------------------------------------------------
  37.