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
}
我看了文檔;複製boost :: shared_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」?
我找不到示例...請幫助。
謝謝。
我想要一個實際的副本,而不是2個指針指向同一個對象當我修改passed_ptr的數據時,cached_ptr的數據應該保持不變 – Thalia 2013-05-07 16:20:58
@Thalia:我擴展了我的答案來涵蓋這個 – 2013-05-07 16:23:23
謝謝,我在提出這個問題之前看到了這個,並試圖用「重置「,但它不可用 – Thalia 2013-05-07 16:25:56