🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1905

re: delete this and class derivation

#1905
From: Denis Lynch
Date: Tue, 05 Sep 1995 05:02
68 lines
2710 bytes
In article <DE5J6J.69B@online.tmx.com.au> tony@online.tmx.com.au (Tony Cook) writes:
> Is the following meant to be legal?
> 

Code edited for conciseness:

> class B1 {
>   public:
>     int JustSomeData;
> };
> 
> class B2 {
>   public:
>     void f() { delete this; } // this might not point at start of D
> };
> 
> class D : public B1, public B2 {
> };
> 
> int main() {
>     D *d = new D;
>     d->f();    
> }
> 

The ARM says this about 'delete':

    The operand of 'delete' must be a pointer returned by 'new'.

In the above example, this is almost certain to be false. The interesting question is:  
under what circumstances can this be guaranteed to be true? I don't know the answer,  
but have had no trouble using a rule like:

   the type of the 'delete'd pointer is the same as the type that was 'new'ed, or 
   is a base class that appears first in the base class list of the next derived 
   class on the way to the type that was 'new'ed, and so forth.

(If you ever wondered about why I don't write standards, that paragraph should clear it  
right up!)


The Working Paper says (5.3.5, item 2):

  ... "the value of the operand of delete shall
  be a pointer to a non-array object created by a new-expression without
  a   new-placement   specification,   or  a  pointer  to  a  sub-object
  (_ intro.object _)  representing  a  base  class  of  such   an   object
  (_ class.derived _),  or  an  expression of class type with a conversion
  function to pointer type (_class.conv,fct_) which yields a pointer  to
  such  an  object."

Which sure makes it sound like the above code should work. So all you need to do is  
wait for comiplers to catch up with the standard!

Thanks for asking that question -- it gave me a reason to look this up in the standard,  
and the existing implementations have caused me lots of grief!

--
Denis Lynch                               dml@bis.trw.com
TRW Business Intelligence Systems      Voice 408.541.6418
Sunnyvale, CA                          FAX   408.541.6401


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <MATT.95Sep04210241@Austern>
Path: arc1-ca.pugleaf.net!newsxfer.itd.umich.edu!agate!agate!Matt
References: <DE5J6J.69B@online.tmx.com.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsxfer.itd.umich.edu!agate!agate!Matt
From: dml@bis.trw.com (Denis Lynch)
Newsgroups: comp.std.c++
Subject: re: delete this and class derivation
Date: 05 Sep 1995 05:02:41 GMT
Organization: TRW Business Intelligence Systems
Approved: Matt@Austern
Message-ID: <MATT.95Sep04210241@Austern>
References: <DE5J6J.69B@online.tmx.com.au>
NNTP-Posting-Host: austern.hip.berkeley.edu