weak_ptr::lock

获取资源的独占的所有权。

shared_ptr<Ty> lock() const;

备注

成员函数返回空的情况对象 *this 是否已过期;否则它返回拥有资源 *this 指向的 shared_ptr Class<Ty;AMP_gt; 对象。

示例

 

// std_tr1__memory__weak_ptr_lock.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct deleter 
    { 
    void operator()(int *p) 
        { 
        delete p; 
        } 
    }; 
 
int main() 
    { 
    std::weak_ptr<int> wp; 
 
     { 
    std::shared_ptr<int> sp(new int(10)); 
    wp = sp; 
    std::cout << "wp.expired() == " << std::boolalpha 
        << wp.expired() << std::endl; 
    std::cout << "*wp.lock() == " << *wp.lock() << std::endl; 
     } 
 
// check expired after sp is destroyed 
    std::cout << "wp.expired() == " << std::boolalpha 
        << wp.expired() << std::endl; 
    std::cout << "(bool)wp.lock() == " << std::boolalpha 
        << (bool)wp.lock() << std::endl; 
 
    return (0); 
    } 
 
  

要求

标头: <memory>

命名空间: std

请参见

参考

weak_ptr Class