i have a shared_ptr and a weak_ptr
typedef boost::weak_ptr<classname> classnamePtr;
typedef boost::shared_ptr<x> xPtr;
how to convert a weak_ptr to a shared_ptr
shared_ptr = weak_ptr;
Xptr = classnameptr; ?????
As already said
boost::shared_ptr<Type> ptr = weak_ptr.lock();
If you do not want an exception or simply use the cast constructor
boost::shared_ptr<Type> ptr(weak_ptr);
This will throw if the weak pointer is already deleted.
You don't convert a weak_ptr
to a shared_ptr
as that would defeat the whole purpose of using weak_ptr
in the first place.
To obtain a shared_ptr
from an instance of a weak_ptr
, call lock
on the weak_ptr
.
Usually you would do the following:
weak_ptr<foo> wp = ...;
if (shared_ptr<foo> sp = wp.lock())
{
// safe to use sp
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With