Quote:According to here,
Quote:
Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.
So the iterators should always remain valid. I don't ever remove individual elements, so deletion is not an issue.
Sure - as you note, a map iterator will remain valid unless that specific element is destroyed.
However, that doesn't protect you from inadvertently erasing that element yourself, from the entire map getting cleared, or from the map going out of scope.
Sure, you're not planning on letting any of these things happen, but that's the problem with C++: we're all quite certain that we won't shoot ourselves in the foot - but eventually, we do :)
Anyway, here's the way I look at it. The iterator issue aside, it would be wise to handle ownership of the pointed-at objects via RAII. This basically means either boost::ptr_map (or something like it), or boost::shared_ptr (or something like it).
In the former case (boost::ptr_map), handing out the pointer itself would be no more or less dangerous than handing out an iterator (or so it seems to me at the moment - there might be more to it than that, but I'm not going to give it much more thought right now), so you might as well just hand out the pointer. In the latter case (boost::shared_ptr), you're golden no matter what; whether you hand out a strong or weak reference, you're (practically) guaranteed a valid pointer to dereference.
In other words, I don't think you have anything to lose by handing out a pointer of some sort rather than an iterator (IMO, of course).