共用方式為


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 Class

weak_ptr::reset