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