Windows Dev Center

Information
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.

weak_ptr::weak_ptr

Constructs a weak_ptr.

weak_ptr();
weak_ptr(const weak_ptr& wp);
template<class Other>
    weak_ptr(const weak_ptr<Other>& wp);
template<class Other>
    weak_ptr(const shared_ptr<Other>& sp);

Other

The type controlled by the argument shared/weak pointer.

wp

The weak pointer to copy.

sp

The shared pointer to copy.

The constructors each construct an object that points to the resource named by the operand sequence.

 

// std_tr1__memory__weak_ptr_construct.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::weak_ptr<int> wp0; 
    std::cout << "wp0.expired() == " << std::boolalpha 
        << wp0.expired() << std::endl; 
 
    std::shared_ptr<int> sp1(new int(5)); 
    std::weak_ptr<int> wp1(sp1); 
    std::cout << "*wp1.lock() == " 
        << *wp1.lock() << std::endl; 
 
    std::weak_ptr<int> wp2(wp1); 
    std::cout << "*wp2.lock() == " 
        << *wp2.lock() << std::endl; 
 
    return (0); 
    } 
 
wp0.expired() == true
*wp1.lock() == 5
*wp2.lock() == 5

Header: <memory>

Namespace: std

Show:
© 2015 Microsoft