Windows Dev Center

The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.


Replaces owned resource.

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);


The type controlled by the argument shared/weak pointer.


The weak pointer to copy.


The shared pointer to copy.

The operators all release the resource currently pointed to by *this and assign ownership of the resource named by the operand sequence to *this. If an operator fails it leaves *this unchanged.


// 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); 
*wp0.lock() == 5
*wp0.lock() == 10
*wp1.lock() == 10

Header: <memory>

Namespace: std

© 2015 Microsoft