home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16407 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  1.6 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!network.ucsd.edu!ucsbcsl!foxtrot!doug
  2. From: doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to keep a member from being deleted thru a pointer?
  5. Message-ID: <6731@ucsbcsl.ucsb.edu>
  6. Date: 17 Nov 92 03:55:47 GMT
  7. References: <1992Nov15.035153.18866@u.washington.edu> <1e567gINNi16@early-bird.think.com>
  8. Sender: root@ucsbcsl.ucsb.edu
  9. Distribution: usa
  10. Organization: Center for Computer Music Research and Composition, UCSB
  11. Lines: 30
  12.  
  13. In article <1e567gINNi16@early-bird.think.com> barmar@think.com (Barry Margolin) writes:
  14. >In article <1992Nov15.035153.18866@u.washington.edu> chuan@stein.u.washington.edu (Chuan Lee) writes:
  15. >>Now if someone does a
  16. >>    delete c.getItem("a member");
  17. >>instead of using
  18. >>    c.removeItem("a member");
  19. >>  So how can I prevent that from happening?
  20. >
  21. >Make Item's destructor private.  Then only members and friends of Item can
  22. >delete instances of Item.
  23. >
  24. >Note, however, that this will also prevent non-members/friends from
  25. >declaring automatic Item variables, since they would have to invoke the
  26. >destructor when the variable's scope ends.
  27.  
  28. I believe you can solve this by doing the following:
  29.  
  30.     const Item* Collection::getItem(const char*) {...}
  31.  
  32. If I am not mistaken, you cannot call delete on a const return value.  Anyone
  33. else know for sure?
  34.  
  35.  
  36.  
  37.  
  38. -- 
  39. Douglas Scott                              (805)893-8352
  40. Center for Computer Music Research and Composition
  41. University of California, Santa Barbara
  42. Internet: (NeXTMail ok)   <doug@foxtrot.ccmrc.ucsb.edu>
  43.