型 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 のオブジェクト。
解説
一つ目のコンストラクターは myptrで _Ptr、割り当てられたオブジェクトに格納されているポインターを格納します。2 つ目のコンストラクターは、_Rightを格納することによって _Rightに格納されているポインターの所有権を譲渡されます。myptrのリリース。
3 つ目のコンストラクターは、2 番目のと同様に動作します。ただし、rightを格納します。ref。ref が _Rightに格納されている参照である myptrのrelease。
テンプレートのコンストラクターは、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;
}
必要条件
ヘッダー : <memory>
名前空間: std