I have an existing variable, e.g.
int a = 3;
How can I now create a boost::shared_ptr
to a
? For example:
boost::shared_ptr< int > a_ptr = &a; // this doesn't work
I have an existing variable, e.g.
int a = 3;
How can I now create a boost::shared_ptr
to a
? For example:
boost::shared_ptr< int > a_ptr = &a; // this doesn't work
What you wrote won't work because the constructor of
shared_ptr
you're looking for isexplicit
, so you'd need to write it like soThe problem with that however, is that
delete
will be called on the stored value ofa_ptr
. Since in your examplea
has automatic storage duration, this is very bad. So we pass in a custom deleter too:An implementation of
noop_deleter
for C++11:C++03 version:
although you should put the variable into a managed pointer on it's creation to do it from an existing pointer.
That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN
If for some reason a function takes shared_ptr and you only have a stack varaible you are better off doing this:
See here:
http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/make_shared.html
also it is worth noting that shared_ptr is in the c++11 standard if you are able using that. You can use auto in combination with make_shared like Herb Sutter notes in the build talk.
You cannot create a boost::shared_ptr for an existing variable. Items stored in a boost::shared_ptr are stored at creation.
You can however make a boost::shared_ptr that is a copy of an existing variable.
For example
Note that you will need to include
<boost/make_shared.hpp>
for make_shared.First, you have an error because
shared_ptr
's will not automatically convert from a pointer of the appropriate type. You have to explicitly state that's what you want to do:You have another problem though. Imagine the effect of this code:
In the first example I gave, this will inevitably happen, even if it's not quite so direct.
shared_ptr
's whole reason for existence is deleting things when all the pointers to it go away. This, of course, will cause all manner of strange behavior.You have two ways of dealing with this issue. One is to create something that can be deleted. The other is to make sure that
shared_ptr
doesn't actually delete the thing it points to. There are pros and cons to each.Making something that can be deleted:
Pros:
Cons:
shared_ptr
will refer to a copy, so modifications toa
will not be reflected in the value of the thing it points to.How to do it:
This is rather similar to (and this will also work):
But it's slightly more efficient.
::boost::make_shared
does some magic to allocate the reference count and the object in contiguous memory which saves on calls to the allocator and improves locality of reference.Making it so that
shared_ptr
doesn't actually delete what it points to:Pros:
shared_ptr
refers toa
, so if you change its value things that access it through the pointer will see the new value.Cons:
shared_ptr
works, which means the people reading your code have to know too.shared_ptr
's that point to it do, then those pointers become dangling, and that's bad.How to do it:
Somewhere outside the function (probably in an anonymous namespace):
And then in the function: