次の方法で共有


shared_ptr::reset

更新 : 2007 年 11 月

所有されたリソースを置き換えます。

void reset();
template<class Other>
    void reset(Other *ptr;);
template<class Other, class D>
    void reset(Other *ptr, D dtor);

パラメータ

  • Other
    引数ポインタによって制御される型。

  • D
    削除子の型。

  • ptr
    コピーするポインタ。

  • dtor
    コピーする削除子。

解説

いずれの演算子も、現在 *this が所有しているリソースの参照カウントをデクリメントし、オペランド シーケンスで指定されたリソースの所有権を *this に割り当てます。参照カウントがゼロに達すると、リソースが解放されます。失敗した場合、*this は変更されません。

使用例

 

// std_tr1__memory__shared_ptr_reset.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct deleter 
    { 
    void operator()(int *p) 
        { 
        delete p; 
        } 
    }; 
 
int main() 
    { 
    std::tr1::shared_ptr<int> sp(new int(5)); 
 
    std::cout << "*sp == " << std::boolalpha 
        << *sp << std::endl; 
 
    sp.reset(); 
    std::cout << "(bool)sp == " << std::boolalpha 
        << (bool)sp << std::endl; 
 
    sp.reset(new int(10)); 
    std::cout << "*sp == " << std::boolalpha 
        << *sp << std::endl; 
 
    sp.reset(new int(15), deleter()); 
    std::cout << "*sp == " << std::boolalpha 
        << *sp << std::endl; 
 
    return (0); 
    } 
 
*sp == 5
(bool)sp == false
*sp == 10
*sp == 15

必要条件

ヘッダー : <memory>

名前空間 : std::tr1

参照

参照

<memory> (TR1)

shared_ptr クラス

shared_ptr::operator=