home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / oop / macapp3 / 657 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.2 KB  |  55 lines

  1. Path: sparky!uunet!pageworks.com!world!eff!sol.ctr.columbia.edu!usc!elroy.jpl.nasa.gov!ames!agate!stanford.edu!apple!applelink.apple.com
  2. From: ALGER@AppleLink.Apple.COM (Alger, Jeff,VCA)
  3. Newsgroups: comp.sys.mac.oop.macapp3
  4. Subject: Re3: C++ (was re: bedrock defe
  5. Message-ID: <728161901.1524811@AppleLink.Apple.COM>
  6. Date: 27 Jan 93 19:03:00 GMT
  7. Sender: daemon@Apple.COM
  8. Organization: AppleLink Gateway
  9. Lines: 44
  10.  
  11. Alan et al,
  12.  
  13. Let's not forget in this trashing of C++ that there are some very good features
  14. of the language that simply aren't used very much, the idioms Larry spoke of
  15. (and I STRONGLY recommend that people attend his talk at MADACON).  As just one
  16. of many examples, consider possibilities for smart pointers.
  17.  
  18. class foo {...};
  19. class PFoo {
  20. private:
  21.     foo* fFoo;
  22. public:
  23. // various constructors, operator=, etc.
  24.     foo* operator-> (void) { return fFoo; }
  25. };
  26.  
  27. Now one can use a PFoo as a direct replacement for a foo* in almost every
  28. circumstance.  It is the same four bytes and operator->, being inline, is the
  29. same overhead as using a foo* directly.  What's the big deal?  Suppose, perhaps
  30. only with a compile-time debug switch on, we make operator-> and other methods
  31. a little more sophisticated:
  32.  
  33. 1. Check to make sure fFoo is non-nil.  This traps nil pointers very
  34. efficiently.
  35. 2. As suggested in my Frameworks article, keep a reference count in each foo,
  36. counting how many PFoos there are out there.  Now, in foo's destructor make
  37. sure the reference count is zero and raise an exception otherwise.  This traps
  38. dangling pointers.
  39. 3. For that matter, when the reference count goes to zero put the foo into a
  40. list of objects that should have been deleted and weren't, thereby trapping
  41. memory leaks.
  42.  
  43. These are just a few of the things that one can do in C++ that have no
  44. convenient counterpart in Pascal or most other efficiently compiled languages.
  45. And if there is a performance concern, it is very easy to bracket them with
  46. conditional compilation switches.  I am not really an apologist for C++ (I'll
  47. pay the price for an OODL any day), but it continues to be the butt of a lot of
  48. undeserved criticism.  Used wisely, I think C++ is actually one of the best
  49. platforms for building robust code that still runs fast.
  50.  
  51. Regards,
  52. Jeff Alger
  53. SBM International
  54.  
  55.