次の方法で共有


auto_ptr クラス

コントロールがあるブロックを離れるときに確実にリソースが自動的に破棄されるように、リソースの周囲にスマート ポインターをラップします。

C++11 以降では、代わりに auto_ptr.unique_ptr 詳細については、クラスを参照してください。unique_ptr auto_ptr は C++11 で非推奨となり、C++17 では削除されました。

throw() および例外処理の詳細については、「例外の仕様 (スロー)」をご覧ください。

構文

class auto_ptr {
    typedef Type element_type;
    explicit auto_ptr(Type* ptr = 0) throw();
    auto_ptr(auto_ptr<Type>& right) throw()
        ;
    template <class Other>
    operator auto_ptr<Other>() throw();
    template <class Other>
    auto_ptr<Type>& operator=(auto_ptr<Other>& right) throw();
    template <class Other>
    auto_ptr(auto_ptr<Other>& right);
    auto_ptr<Type>& operator=(auto_ptr<Type>& right);
    ~auto_ptr();
    Type& operator*() const throw();
    Type * operator->()const throw();
    Type *get() const throw();
    Type *release()throw();
    void reset(Type* ptr = 0);
};

パラメーター

right
既存のリソースの取得元となる auto_ptr

ptr
格納されたポインター を置換するように指定されたポインター。

解説

このクラス テンプレートは、割り当てられたオブジェクトを指し示す auto_ptr と呼ばれるスマート ポインターを記述します。 このポインターは、null であるか、new によって割り当てられたオブジェクトを指定する必要があります。 auto_ptr は、格納されている値が別のオブジェクトに割り当てられている場合、所有権を転送します。 (転送後に格納された値を null ポインターに置き換えます)。デストラクターは、 auto_ptr<Type> 割り当てられたオブジェクトを削除します。 auto_ptr<Type> は、コントロールがブロックを離れるとき、例外がスローされる場合であっても、割り当てられたオブジェクトが確実に自動的に削除するようにします。 同じオブジェクトを所有する 2 つの auto_ptr<Type> オブジェクトを作成しないでください。

auto_ptr<Type> オブジェクトを関数呼び出しの引数として値渡しで渡せます。 標準 auto_ptr ライブラリ コンテナーの要素にすることはできません。 C++ 標準ライブラリ コンテナーを使用して一連の auto_ptr<Type> オブジェクトを確実に管理することはできません。

メンバー

コンストラクター

名前 説明
auto_ptr auto_ptr 型のオブジェクトのコンストラクター。

Typedefs

名前 説明
element_type この型は、テンプレート パラメーター Typeのシノニムです。

関数

名前 説明
get このメンバー関数は、格納されているポインター myptr を返します。
release このメンバーは、格納されたポインター myptr を null ポインターで置換して、以前に格納されたポインターを返します。
reset このメンバー関数は、関数呼び出しの結果として格納されているポインター値 myptr が変化する場合に限り、式 delete myptr を評価します。 その後、格納されたポインターを ptr で置換します。

演算子

名前 説明
operator= 1 つの auto_ptr オブジェクトから別のオブジェクトに所有権を転送する代入演算子。
operator* auto_ptr 型のオブジェクトの逆参照演算子。
operator-> メンバーにアクセスできるようにする演算子。
operator auto_ptr<Other> 1 つの種類の auto_ptr から別の種類の auto_ptr にキャストします。
operator auto_ptr_ref<Other> auto_ptr から auto_ptr_ref にキャストします。

auto_ptr

auto_ptr 型のオブジェクトのコンストラクター。

explicit auto_ptr(Type* ptr  = 0) throw();

auto_ptr(auto_ptr<Type>& right) throw();

auto_ptr(auto _ptr_ref<Type> right) throw();

template <class Other>
auto _ptr(auto _ptr<Other>& right) throw();

パラメーター

ptr
auto_ptr がカプセル化するオブジェクトへのポインター。

right
コンストラクターによってコピーされる auto_ptr オブジェクト。

解説

最初のコンストラクターは、割り当てられたオブジェクトへの格納されたポインターを格納ptrmyptrします。 2 番目のコンストラクターは、right に格納されているポインターの所有権を転送します。これは right. releasemyptr に格納することで行います。

3 番目のコンストラクターは 2 番目と同じ動作をしますが、right. ref. releasemyptr に格納します。refright に格納されている参照です。

ポインターを暗黙的にポインターに変換Typeできる場合Other、テンプレート コンストラクターは 2 番目のコンストラクターと同じように動作します。

// auto_ptr_auto_ptr.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class Int
{
public:
   Int(int i)
   {
      cout << "Constructing " << ( void* )this  << endl;
      x = i;
      bIsConstructed = true;
   };
   ~Int( )
   {
      cout << "Destructing " << ( void* )this << endl;
      bIsConstructed = false;
   };
   Int &operator++( )
   {
      x++;
      return *this;
   };
   int x;
private:
   bool bIsConstructed;
};

void function ( auto_ptr<Int> &pi )
{
   ++( *pi );
   auto_ptr<Int> pi2( pi );
   ++( *pi2 );
   pi = pi2;
}

int main( )
{
   auto_ptr<Int> pi ( new Int( 5 ) );
   cout << pi->x << endl;
   function( pi );
   cout << pi->x << endl;
}
Constructing 00311AF8
5
7
Destructing 00311AF8

element_type

この型は、テンプレート パラメーター Typeのシノニムです。

typedef Type element  _type;

get

このメンバー関数は、格納されているポインター myptr を返します。

Type *get() const throw();

戻り値

格納されているポインター myptr

// auto_ptr_get.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;

class Int
{
public:
   Int(int i)
   {
      x = i;
      cout << "Constructing " << ( void* )this  << " Value: " << x << endl;
   };
   ~Int( )
   {
      cout << "Destructing " << ( void* )this << " Value: " << x << endl;
   };

   int x;

};

int main( )
{
   auto_ptr<Int> pi ( new Int( 5 ) );
   pi.reset( new Int( 6 ) );
   Int* pi2 = pi.get ( );
   Int* pi3 = pi.release ( );
   if (pi2 == pi3)
      cout << "pi2 == pi3" << endl;
   delete pi3;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

operator=

1 つの auto_ptr オブジェクトから別のオブジェクトに所有権を転送する代入演算子。

template <class Other>
    auto_ptr<Type>& operator=(auto_ptr<Other>& right) throw();
auto_ptr<Type>& operator=(auto_ptr<Type>& right) throw();
auto_ptr<Type>& operator=(auto_ptr_ref<Type> right) throw();

パラメーター

right
auto_ptr 型オブジェクト。

戻り値

auto_ptr<Type> 型のオブジェクトへの参照。

解説

この割り当ては、割り当ての結果として格納されているポインター値 myptr が変化する場合に限り、式 delete myptr を評価します。 次に、右に格納されたポインターの所有権を、右格納することで転送します。myptrrelease*this が返されます。

メンバー演算子の使用例については、次を参照してください auto_ptr

operator*

auto_ptr 型のオブジェクトの逆参照演算子。

Type& operator*() const throw();

戻り値

ポインターが所有する型 Type のオブジェクトへの参照。

解説

間接演算子は *get を返します。 つまり、格納されたポインターは、null にすることはできません。

メンバー関数の使用方法の例については、次を参照してください auto_ptr

operator->

メンバーにアクセスできるようにする演算子。

Type * operator->() const throw();

戻り値

auto_ptr を所有するオブジェクトのメンバー。

解説

選択演算子は、式 ap-member> が ( とap同じように動作するように、 を返します。get( ) get() )->member,クラスのauto_ptr<>Typeオブジェクトを指定apします。 そのため、格納されているポインターを null にすることはできず、Type はメンバー member を持つクラス、構造体、または共用体型にする必要があります。

メンバー関数の使用方法の例については、次を参照してください auto_ptr

operator auto_ptr<Other>

1 つの種類の auto_ptr から別の種類の auto_ptr にキャストします。

template <class Other>
operator auto _ptr<Other>() throw();

戻り値

型キャスト演算子は Other>(*this) を返しますauto_ptr<

// auto_ptr_op_auto_ptr.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;
int main()
{
   auto_ptr<int> pi ( new int( 5 ) );
   auto_ptr<const int> pc = ( auto_ptr<const int> )pi;
}

operator auto_ptr_ref<Other>

auto_ptr から auto_ptr_ref にキャストします。

template <class Other>
operator auto _ptr  _ref<Other>() throw();

戻り値

型キャスト演算子は、auto_ptr_ref>(*this)Other<を返します。

// auto_ptr_op_auto_ptr_ref.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class C {
public:
    C(int _i) : m_i(_i) {
    }
    ~C() {
        cout << "~C:  " << m_i << "\n";
    }
    C &operator =(const int &x) {
        m_i = x;
        return *this;
    }
    int m_i;
};
void f(auto_ptr<C> arg) {
};
int main()
{
    const auto_ptr<C> ciap(new C(1));
    auto_ptr<C> iap(new C(2));

    // Error: this implies transfer of ownership of iap's pointer
    // f(ciap);
    f(iap); // compiles, but gives up ownership of pointer

            // here, iap owns a destroyed pointer so the following is bad:
            // *iap = 5; // BOOM

    cout << "main exiting\n";
}
~C:  2
main exiting
~C:  1

release

このメンバーは、格納されたポインター myptr を null ポインターで置換して、以前に格納されたポインターを返します。

Type *release() throw();

戻り値

以前に格納されたポインター。

解説

このメンバーは、格納されたポインター myptr を null ポインターで置換して、以前に格納されたポインターを返します。

// auto_ptr_release.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;

class Int
{
public:
    Int(int i)
    {
        x = i;
        cout << "Constructing " << (void*)this << " Value: " << x << endl;
    };
    ~Int() {
        cout << "Destructing " << (void*)this << " Value: " << x << endl;
    };

    int x;

};

int main()
{
    auto_ptr<Int> pi(new Int(5));
    pi.reset(new Int(6));
    Int* pi2 = pi.get();
    Int* pi3 = pi.release();
    if (pi2 == pi3)
        cout << "pi2 == pi3" << endl;
    delete pi3;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

reset

このメンバー関数は、関数呼び出しの結果として格納されているポインター値 myptr が変化する場合に限り、式 delete myptr を評価します。 その後、格納されたポインターを ptr で置換します。

void reset(Type* ptr = 0);

パラメーター

ptr
格納されたポインター myptr を置換するように指定されたポインター。

// auto_ptr_reset.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class Int
{
public:
    Int(int i)
    {
        x = i;
        cout << "Constructing " << (void*)this << " Value: " << x << endl;
    };
    ~Int()
    {
        cout << "Destructing " << (void*)this << " Value: " << x << endl;
    };

    int x;
};

int main()
{
    auto_ptr<Int> pi(new Int(5));
    pi.reset(new Int(6));
    Int* pi2 = pi.get();
    Int* pi3 = pi.release();
    if (pi2 == pi3)
        cout << "pi2 == pi3" << endl;
    delete pi3;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

関連項目

unique_ptr Class