Using Visitor and smart pointers
I've got the Loki Acyclic Visitor setup working in my code,
but there's one thing I can't figure out . . .
Right now, I'm keeping a collection (currently a map) of
pointers to base objects. In my visitors, I'd like to
create and pass a smart pointer off to another class
hierarchy. I can't figure out a clever way to do this.
i.e.
std::map<int, Element *> m;
Some_Visitor v;
std::map<int, Element *>::iterator mi = m.find (7);
if (mi != m.end ()) {
mi->second->Accept (v);
m.erase (mi);
}
now, in Some_Visitor, I have
void
Some_Visitor::Visit (Concrete_Element &ce)
{
// here, I could say
pass_ptr (boost::shared_ptr<Element> (&ce));
}
and that would all work, because there's no other
smart pointer pointing at ce (I was keeping raw
pointers in my map).
But let's say I wanted the defintion of the map to
be std::map<int, boost::shared_ptr<Element> >.
now, when I get into the Visitor, my Visit function
only has a reference to the Element, and if I create
a new shared_ptr there, I'm in for a world of hurt
because the one way back in the Accept scope is going
to delete the ce object when the iterator goes out of
scope.
Is there a way to use shared_ptrs across the Visitor
framework?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]