typedef boost::shared_ptr<SomeData> data_ptr;
data_ptr cached_ptr; // class member
bool someWork(data_ptr& passed_ptr)
{
// must copy passed_ptr = cached_ptr under some conditions
// without pointing at the same memory
// I saw somewhere that I should do
// passed_ptr.reset(new SomeData(???))
// I don't have a "reset" on passed_ptr
}
我查看了文档;
复制和转换构造函数
shared_ptr(shared_ptr const & r); // never throws
template<class Y> shared_ptr(shared_ptr<Y> const & r); // never throws
Requires: Y* should be convertible to T*.
Effects: If r is empty, constructs an empty shared_ptr; otherwise,
constructs a shared_ptr that shares ownership with r.
我不知道它是如何工作的——是这样的吗?
passed_ptr = shared_ptr(cached_ptr);
? const 会去哪里?他们共享所有权意味着什么?那么它不是副本,如果我修改“passed_ptr”,更改会影响“cached_ptr”吗?
我找不到示例...请帮助。
谢谢你。