Condividi tramite


auto_ptr::auto_ptr

Il costruttore per oggetti di tipo 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( );

Parametri

  • _Ptr
    Il puntatore all'oggetto che incapsula auto_ptr.

  • _Right
    L'oggetto auto_ptr da copiare dal costruttore.

Note

Il primo file _Ptr in myptr, il puntatore l'archiviazione nell'oggetto allocato.Il secondo costruttore la proprietà del puntatore archiviato in _Right, archiviando _Right.versione in myptr.

Il terzo costruttore si comporta come il secondo, con la differenza che archivia right.ref.release in myptr, dove ref è il riferimento archiviato in _Right.

Il costruttore del modello si comporta come il secondo costruttore, a condizione che un puntatore a altro in modo implicito essere convertito in un puntatore a Tipo.

Esempio

// 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;
}
  

Requisiti

intestazione: <memory>

Spazio dei nomi: deviazione standard

Vedere anche

Riferimenti

auto_ptr Class