home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!van-bc!rsoft!agate!stanford.edu!ames!sun-barr!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: Explicit Calls to Destructors (SunCC=ok, gcc=not)
- Summary: use "this->~T()" ("this->T::~T() to workaround gcc bug)
- Keywords: destructors
- Message-ID: <79121@ut-emx.uucp>
- Date: 4 Sep 92 19:31:23 GMT
- References: <gnusenet2234@h.cs.wvu.wvnet.edu>
- Reply-To: jamshid@emx.utexas.edu
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 61
-
- In article <gnusenet2234@h.cs.wvu.wvnet.edu> schiebel@cs.wvu.wvnet.edu (Darrell Schiebel) writes:
- > In the Sun (SC1.0 w/ cfront 2.1 (I think)), an explicit call
- > to a class' destructor from within a one of the class' member
- > functions works:
- >
- > void Tess::cleanup(Bool abort) {
- > Tess::~Tess();
- > }
-
- That's not the correct way to call a dtor. It probably doesn't work
- under later versions of cfront.
-
- > However, in gcc-2.2.2 this does not work ...
- >
- > Other nonworking variations:
- >
- > this->~Tess(); AND
- > (*this).~Tess(); WITH THE ERROR MESSAGE: ...
-
- Both of these are correct -- you've encountered a gcc-2.2.2 bug.
- Btw, it is also legal (since the ARM) to explicitly destroy built-in
- types (it's a no-op, of course). This makes it easier to instantiate
- templates which explicitly destroy their objects with built-in types.
- Note, BC++ 3.0 didn't implement this feature, and BC++ 3.1 only allows
- one form of the syntax:
-
- int i;
- int* p = &i;
- i.~int(); // correct, but BC++ error
- p->~int(); // correct, but BC++ error
- i.int::~int(); // correct, but BC++ error
- p->int::~int(); // correct and BC++ allows
-
- The difference between:
- p->~T();
- and
- p->T::~T();
- is that the second overrides the "virtualness" of the dtor (if it is
- virtual). You should therefore almost always use the first form. Besides,
- it's less characters and not any slower if ~T() isn't virtual.
-
- > and finally:
- >
- > ~Tess(); WITH:
-
- You must explicitly use a "." or "->" whenever calling the dtor, even
- from within a member function. 'this' is not assumed. The above
- tries to create a temporary Tess and call it's unary ~ operator (see
- ARM 12.4).
-
- > Is there some way to get around this in gcc? Its exactly what
- > I want to do; I don't want to force users of my class to call
- > parent ``cleanup'' functions, in children. Explicit calls to
- > destructors is permitted, ARM p.278, sec 12.4.
-
- I think someone said "this->Tess::~Tess()" works under gcc. Just
- remember that this will not call the destructor of derived class
- (I'm assuming ~Tess() is declared virtual).
-
- Jamshid Afshar
- jamshid@emx.utexas.edu
-