次の方法で共有


shared_ptr::shared_ptr

shared_ptr を構築します。

shared_ptr();
shared_ptr(nullptr_t); 
shared_ptr(const shared_ptr& sp);
shared_ptr(shared_ptr&& sp); 
template<class Other>
    explicit shared_ptr(Other *ptr);
template<class Other, class D>
    shared_ptr(Other *ptr, D dtor);
template<class D>
    shared_ptr(nullptr_t ptr, D dtor);
template<class Other, class D, class A>
    shared_ptr(Other *ptr, D dtor, A alloc);
template<class D, class A>
    shared_ptr(nullptr_t ptr, D dtor, A alloc);
template<class Other>
    shared_ptr(const shared_ptr<Other>& sp);
template<class Other>
    shared_ptr(const weak_ptr<Other>& wp);
template<class &>
    shared_ptr(std::auto_ptr<Other>& ap);
template<class &>
    shared_ptr(std::auto_ptr<Other>&& ap);
template<class Other, class D>
    shared_ptr(unique_ptr<Other, D>&& up);
template<class Other>
    shared_ptr(const shared_ptr<Other>& sp, Ty *ptr);
template<class Other, class D>
    shared_ptr(const unique_ptr<Other, D>& up) = delete;

パラメーター

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

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

  • D
    削除子の型。

  • A
    アロケーターの型。

  • dtor
    削除子。

  • ator
    アロケーター。

  • sp
    コピーするスマート ポインター。

  • wp
    ウィーク ポインター。

  • ap
    コピーする自動ポインター。

解説

それぞれのコンストラクターは、オペランド シーケンスによって指定されたリソースを所有するオブジェクトを構築します。コンストラクター shared_ptr(const weak_ptr<Other>& wp) は、wp.expired() の場合に、bad_weak_ptr クラス 型の例外オブジェクトをスローします。

使用例

 

// std_tr1__memory__shared_ptr_construct.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct deleter 
    { 
    void operator()(int *p) 
        { 
        delete p; 
        } 
    }; 
 
int main() 
    { 
    std::shared_ptr<int> sp0; 
    std::cout << "(bool)sp0 == " << std::boolalpha 
        << (bool)sp0 << std::endl; 
 
    std::shared_ptr<int> sp1(new int(5)); 
    std::cout << "*sp1 == " << *sp1 << std::endl; 
 
    std::shared_ptr<int> sp2(new int(10), deleter()); 
    std::cout << "*sp2 == " << *sp2 << std::endl; 
 
    std::shared_ptr<int> sp3(sp2); 
    std::cout << "*sp3 == " << *sp3 << std::endl; 
 
    std::weak_ptr<int> wp(sp3); 
    std::shared_ptr<int> sp4(wp); 
    std::cout << "*sp4 == " << *sp4 << std::endl; 
 
    std::auto_ptr<int> ap(new int(15)); 
    std::shared_ptr<int> sp5(ap); 
    std::cout << "*sp5 == " << *sp5 << std::endl; 
 
    return (0); 
    } 
 
  

必要条件

ヘッダー : <memory>

名前空間: std

参照

関連項目

shared_ptr クラス