home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!Germany.EU.net!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: Is it possible to use 'delete this' ?
- In-Reply-To: rmartin@thor.Rational.COM's message of Sun, 10 Jan 1993 14:12:48 GMT
- Message-ID: <KANZE.93Jan12153048@slsvdnt.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <rmartin.725129268@thor> <BzqE5s.6Lr@comp.vuw.ac.nz> <rmartin.725581109@thor>
- <C000qE.D3J@comp.vuw.ac.nz> <rmartin.726675168@thor>
- Date: 12 Jan 93 15:30:48
- Lines: 55
-
- In article <rmartin.726675168@thor> rmartin@thor.Rational.COM (Bob
- Martin) writes:
-
- |> robertd@kauri.vuw.ac.nz (Robert Davies) writes:
-
- |> |The problem I had with Bob's first reply was that he didn't answer the
- |> |question: is "delete this" legal.
-
- |> To my knowledge, the syntax is legal. (Steve Clamage: Can you
- |> comment on legality?)
-
- |> |Then it would be appropriate to give a warning. Incidentally, is the warning
- |> |about is causing difficulty to some compilers just a "gut feeling" or are
- |> |there specific examples?
-
- |> No specific examples. I give the warning because I have seen no words
- |> in the ARM to the effect that the code generated by the compiler must
- |> assume that the object may not exist during a member function call.
-
- |> |Bob's warning about "delete this" seems to apply to any use of "delete" by
- |> |an entity that didn't do the corresponding "new". Otherwise there is always
- |> |the risk that the "delete" will be applied to something not created by
- |> |"new". Is this another safe programming rule?
-
- |> No. And my warning is only about the dangers of "delete this".
- |> "delete this" can only be appropriate for objects that are "always"
- |> allocated from the heap. Unfortunately, there is no way for an object
- |> to insure this.
-
- Granted, there is *no* way for an object alone to ensure that it was
- allocated on the heap. But not all classes exist in isolation. If a
- class is declared in the private part of another class, then it can
- only be used by the containing class. In this situation, the author
- of the *two* classes *can* control where all of the instances of the
- contained class come from, and using "delete this" would seem in this
- case to be a reasonable solution.
-
- As a practical example, a tree class contains the definition of its
- nodes. The node class(es) themselves contain a cleanup function,
- which calls cleanup on the children, and then delete this. The
- destructor for the tree simply calls cleanup on the root node. (I
- have actually used code like this, and find it quite practical.)
-
- On the other hand, I am very sceptical of using delete this in a class
- which is directly accessible to everyone, for the very reason that Bob
- states. If a user can do something, you must program under the
- assumption that he will, and if the class is accessable to the user,
- he can (and sooner or later will) declare an object of that class on
- the stack. So I would consider it very bad programming style to use
- "delete this" in any such class.
- --
- James Kanze email: kanze@us-es.sel.de
- GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
- Conseils en informatique industrielle --
- -- Beratung in industrieller Datenverarbeitung
-