有没有办法可以让共享指针指向不同的内存位置而不释放内存。目前由它指出
请考虑代码:
#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);
// This leads to a compiler error
ps = boost::make_shared<int>(q);
std::cout << *p << std::endl;
std::cout << *q << std::endl;
return 0;
}
答案 0 :(得分:1)
你不能。
当然,您可以释放并重新附加,同时将删除更改为无操作
说实话,看起来你只是想要
ps = boost::make_shared<int>(*q);
打印(live on Coliru):
0
20