Topic: iterators & delete


Author: frode@auticon.no (Frode Nilsen)
Date: 1996/11/01
Raw View
In some code I made I used something like this

class Record : private list<Field*>

And then stored away som subclasses of Field in this list.

on deletion of Record I iterated through the list and deleted each
Field.

But, and here is my question

Since "The semantics of iterators are a generalization of the
semantics of C++ pointers" shouldn't it be possible to do just a

delete Record.begin()[];


Any comments, references to STL or C++ draft standard?
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Nathan Myers <ncm@cantrip.org>
Date: 1996/11/02
Raw View
Frode Nilsen wrote:

> Since "The semantics of iterators are a generalization of the
> semantics of C++ pointers" shouldn't it be possible to do just a
>
> delete Record.begin()[];

The semantics of iterators are a generalization of the semantics
of C++ pointers.  The interface to iterators is an abstration of
the interface to C++ pointers.  There are plenty of things you can
do with pointers, but not with iterators.

Nathan Myers
ncm@cantrip.org
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: shankel@crl.com.___.no.automatic.list.generators (Jason Shankel)
Date: 1996/11/05
Raw View
In article <55cghb$efi@frysja.sn.no>, frode@auticon.no says...
>Since "The semantics of iterators are a generalization of the
>semantics of C++ pointers" shouldn't it be possible to do just a
>
>delete Record.begin()[];

First, I think you mean:
delete[] Record.begin()...

Second, nope.
Just as you should not be able to do this:

Field *Record[10];
delete[] Record;

While each of the Field *'s pointed to by Record is indeed allocated on the
heap, Record itself is not, thus begin() does is not deletable.

In reality, of course, begin is probably actually on the heap, and it will get
deleted by list's destructor, so it follows the semantics of a stack pointer.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]