24 May
2007
24 May
'07
6:01 p.m.
Daniel Gehriger wrote:
I happen to work with custom containers whose iterators are invalidated when certain member functions are called on the contained objects. So I typically use a loop pattern as follows:
------------------- for (Coll::iterator it = col.begin(); it != col.end(); /* noop */) { Coll::value_type& o = *(it++); o.destroy(); } -------------------
Aren't you asking for trouble here? If the call into a member function of a contained object invalidates iterators of the container that holds it and you're in the process of iterating over that container and calling such a function, then how do you know that the new value for 'it' is still points to a valid address within the container itself? -Chris