• Advertisement
Sign in to follow this  

so, what about boost::ptr_vector

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

if i use a boost::ptr_vector, the pointers in the ptr_vector delete, like shared_ptr (when the pointer is out of scope) or i need to do a std::vector<shared_ptr> to get that behavior thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The difference between ptr_vector<X> and vector<shared_ptr<X> > is that for the former, you need X to be clonable i.e. you need to provide new_clone() and delete_clone() functions if your hierarchy is polymorphic.

It's a little more work, but ptr_vector is often more efficient.

Another solution, if you'll permit the plug, would be to use a vector<value_ptr<X> >. value_ptr is designed to do the cloning for you. Arguably this is the easiest to use, though ptr_vector is likely more efficient.

Share this post


Link to post
Share on other sites
thanks for the help! @the_edd do you have an example of new_clone() and delete_clone() functions or some webpage with a good tutorial (i'm already read the reference in boost)

Share this post


Link to post
Share on other sites
I'm afraid I don't have an example as I've honestly never use any of boost's pointer containers. I imagine you just create an overload of new_clone() and delete_clone() to call your virtual copy function.

Perhaps something like:


base *new_clone(cont base &b)
{
return b.clone();
}



I'm just going by what it says in the docs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement