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

要求

页眉: <内存>

命名空间: std

请参见

参考

auto_ptr 类