auto_ptr クラス
コントロールがあるブロックを離れるときに確実にリソースが自動的に破棄されるように、リソースの周囲にスマート ポインターをラップします。
より高機能なこの unique_ptr クラスは auto_ptr に代わるものです。 詳細については、「unique_ptr クラス」をご覧ください。
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
| Name | 説明 |
|---|---|
| element_type | この型は、テンプレート パラメーター Type のシノニムです。 |
関数
| Name | 説明 |
|---|---|
| get | このメンバー関数は、格納されているポインター myptr を返します。 |
| release | このメンバーは、格納されたポインター myptr を null ポインターで置換して、以前に格納されたポインターを返します。 |
| reset | このメンバー関数は、関数呼び出しの結果として格納されているポインター値 myptr が変化する場合に限り、式 delete myptr を評価します。 その後、格納されたポインターを ptr で置換します。 |
演算子
| Name | 説明 |
|---|---|
| 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 オブジェクト。
注釈
最初のコンストラクターは、ptr を myptr (割り当てられたオブジェクトへの格納されたポインター) に格納します。 2 番目のコンストラクターは、right に格納されているポインターの所有権を転送します。これは right. release を myptr に格納することで行います。
3 番目のコンストラクターは 2 番目と同じ動作をしますが、right. ref. release を myptr に格納します。ref は right に格納されている参照です。
テンプレート コンストラクターは、ポインター Other を暗黙的にポインターに変換できる場合、2 番目のコンストラクターと同じように動作します Type。
例
// 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 を評価します。 その後、right に格納されているポインターの所有権を転送します。これは、right.release を myptr に格納することで行います。 *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( ) )->member と同じように動作するように、get( ) を返します。ap はクラス auto_ptr<Type> のオブジェクトです。 そのため、格納されているポインターを 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();
戻り値
型キャスト演算子は、Other>( *this) auto_ptr_ref<返します。
例
// 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