boost - Giving up ownership of a memory without releasing it by shared_ptr -
is there way can make shared pointer point different memory location without releasing memory.pointed currently
please consider code:
#include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> #include <iostream> int main() { int *p = new int(); *p = 10; int *q = new int(); *q = 20; boost::shared_ptr<int> ps(p); // leads compiler error ps = boost::make_shared<int>(q); std::cout << *p << std::endl; std::cout << *q << std::endl; return 0; }
you can't.
of course can release , reattach, while changing deleter no-op
to honest, looks you'd want
ps = boost::make_shared<int>(*q);
prints (live on coliru):
0 20
Comments
Post a Comment