weak_ptr::operator=
表單取代特定資源。
weak_ptr& operator=(const weak_ptr& wp);
template<class Other>
weak_ptr& operator=(const weak_ptr<Other>& wp);
template<class Other>
weak_ptr& operator=(const shared_ptr<Other>& sp);
參數
Other
引數會控制的型別共用/弱式指標。wp
複製的弱式指標。sp
複製的共用指標。
備註
運算子全部釋出目前指向 *this 並由運算元序列指派資源具名的擁有權的 *this。 如果運算子失敗它保留 *this 未變更。
範例
// std_tr1__memory__weak_ptr_operator_as.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
int main()
{
std::shared_ptr<int> sp0(new int(5));
std::weak_ptr<int> wp0(sp0);
std::cout << "*wp0.lock() == " << *wp0.lock() << std::endl;
std::shared_ptr<int> sp1(new int(10));
wp0 = sp1;
std::cout << "*wp0.lock() == " << *wp0.lock() << std::endl;
std::weak_ptr<int> wp1;
wp1 = wp0;
std::cout << "*wp1.lock() == " << *wp1.lock() << std::endl;
return (0);
}
需求
標頭: <memory>
命名空間: std