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