Sign in to follow this  
juglar

so, what about boost::ptr_vector

Recommended Posts

juglar    148
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
the_edd    2109
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
juglar    148
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
the_edd    2109
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this