auto_ptr::auto_ptr

Der Konstruktor für Objekte des Typs 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( );

Parameter

  • _Ptr
    Der Zeiger auf das Objekt, das auto_ptr kapselt.

  • _Right
    Das vom Konstruktor kopiert werden auto_ptr-Objekt.

Hinweise

Der erste Konstruktor speichert _Ptr in myptr, den gespeicherten Zeiger auf den zugeordneten Objekt.Der zweite Konstruktor überträgt den Besitz des Zeigers, der in _Right gespeichert wird, indem er _Right speichert.Version in myptr.

Der dritte Konstruktor verhält sich genauso wie das zweite, außer dass speichert Rechts.ref.release in myptr, in dem ref der Verweis ist, der in _Right gespeichert wird.

Der Vorlagenkonstruktor verhält sich genauso wie der zweite Konstruktor, vorausgesetzt, dass ein Zeiger auf Andere in einen Zeiger auf Typ implizit konvertiert werden kann.

Beispiel

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

Anforderungen

Header: <memory>

Namespace: std

Siehe auch

Referenz

auto_ptr Class