Re: What is the best way to clear a std::set containing dynamically allocated pointers?
On Wed, 14 Nov 2007 16:51:39 CST, acehreli@gmail.com
<acehreli@gmail.com> wrote:
On Nov 13, 6:16 pm, Olivier Langlois <olangl...@sympatico.ca> wrote:
....
Normally with any other containers, I would just traverse it to delete
all the elements and then call clear() on the container.
As became evident from the replies so far, there is no clear solution
to keeping plain pointers in containers.
It's not evident to me yet ...
You haven't mentioned the
following case, but I think it is a problem of yours:
void foo()
{
MySet mySet = makeSet(/* ... */);
useSet(mySet);
// you will not get here if there was an exception
deletePointers(mySet);
}
There is no guarantee that you will get to the traversal code.
I assume you intend MySet to be a typedef for std::set<T>, not a class
with a suitable destructor.
I cannot speak for Olivier, but when I store raw pointers in a
container, the container is always a member, owned by some object, and
there is a destructor which deletes whatever needs to be deleted.
This works nicely for most containers. I see no harm in it.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]