home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!UB.com!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: delete this???
- Message-ID: <rmartin.715616535@thor>
- Date: 4 Sep 92 14:22:15 GMT
- References: <20146@plains.NoDak.edu>
- Sender: news@Rational.COM
- Lines: 36
-
- heilig@plains.NoDak.edu (Zachary Heilig) writes:
-
- |I saw the previous thread about calling the destructor in member functions,
- |and was wondering how legal the following code would be:
-
-
- |void foo::remove( int call_destruct )
- |{
- | if( call_destruct ) delete this;
- |}
-
-
- |or, should I just call the destructor function?
-
- |any ideas?
-
- The syntax is correct, but the practice is ill-advised. There are
- some possible ambiguities when an object deletes itself. They have to
- do with the fact that the member function for that object is still
- executing, even though the object has been destroyed. If any of the
- objects variables, or its vtbl, get accessed on the way out of the
- call, then trouble can ensue. So your best bet is never to use the
- structure: delete this. (And also avoid goto)
-
- By the way, calling the destructor is also legal syntax: foo::~foo();
- But the result may not be what you expect. Calling the destructor
- executes the code that you wrote for the destructor, but it does not
- destroy the object. Moreover, when the object is finally destroyed,
- the destructor will be called again. So, take care.
-
-
- --
- 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++
-