次の方法で共有


weak_ptr::lock

リソースの排他的所有権を取得します。

shared_ptr<Ty> lock() const;

解説

このメンバー関数は、*this の有効期限が切れている場合、空の shared_ptr オブジェクトを返します。それ以外の場合は、*this が指し示すリソースを所有する shared_ptr クラス<Ty> オブジェクトを返します。

使用例

 

// 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 クラス