home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Is it possible to use 'delete this' ?
- Message-ID: <rmartin.725129268@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <Dy93VB2w165w@bwhwob.escape.de>
- Date: Wed, 23 Dec 1992 16:47:48 GMT
- Lines: 31
-
- bernd@bwhwob.escape.de (Bernd Wiegmann) writes:
-
- >Hi,
-
- >Is it possible to use 'delete this' as the last statement in a
- >method of a class ?
-
- In general, "delete this" is a bad idea. I don't beleive you can
- depend upon all the compiler vendors to generate code which is immune
- to losing the object before the methods are complete. Also, you run
- into difficulties if the object was not allocated with new, or if you
- overload new and delete and then allocate an array of objects.
-
- In general, an object should not be privy to the method used to
- allocate it. It is the user of the object that decides whether it
- should be dynamic, auto or static. There is no good way for an object
- to detect which of these allocation methods were used. Morover, even
- if you are absolutely certain that the object is dynamically
- allocated, you still can't be absolutely sure which dynamic allocation
- method was used. Arrays of objects get deleted in a different way
- than individual objects. You cannot allocate an array and then delete
- the individuals.
-
- So, in short, avoid "delete this" where possible.
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-