weak_ptr::operator=

replaces特定资源。

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