次の方法で共有


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

参照

関連項目

weak_ptr クラス

weak_ptr::reset