共用方式為


auto_ptr::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 物件。

備註

第一個建構函式在 myptr儲存 _Ptr ,已配置物件的儲存的指標。 第二個建構函式會將 _Right儲存的指標的擁有權,將 _Right。在 myptr發行

第三個建構函式一般運作方式和第二個相同,不過,前者會儲存 right。ref。在 myptrrelease , ref 是 _Right儲存的參考。

,在 [其他] 指標可以隱含轉換為 [型別]指標範本建構函式正常運作方式和第二個建構函式相同。

範例

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

請參閱

參考

auto_ptr Class