Changing a Container while using Visitor

Posted by Florian on Stack Overflow See other posts from Stack Overflow or by Florian
Published on 2010-04-15T09:33:23Z Indexed on 2010/04/15 9:43 UTC
Read the original article Hit count: 428

Filed under:
|
|
|

Hi everyone,

I implemented the Visitor pattern in C++ using a STL-like iterator for storing the Visitor's current position in the container. Now I would like to change the container while I iterate over it, and I'm especially interested in deleting items from the container, even the one I'm currently visiting.

Now obviously this will invalidate the Visitors internal iterator, because it was pointing to exactly this item. Currently, I store a list of all iterators in the container and update them, as soon as anything is added to or removed from the list. So in a way this is similar to the Observer pattern applied to the iterator (as Observer) and the list (as Observable).

Alternatively I considered having the visitor() methods return some hint to the Visitor about what happend to the current item and how to proceed iterating, but that doesn't sound like such a good idea either, because the visit() implementation shouldn't really care about finding the next item.

So, my question is: What's the best way to keep a visitor working, even when items are added to the container or removed from it.

Regards, Florian

© Stack Overflow or respective owner

Related posts about c++

Related posts about visitor