共用方式為


auto_ptr::reset

因為函式呼叫,,,才會儲存的指標值 myptr 變更成員函式評估運算式 deletemyptr,不過,。 它會 ptr然後取代儲存的指標。

void reset(
   Type* _Ptr = 0
);

參數

  • _Ptr
    指定指標取代儲存的指標 myptr

範例

// auto_ptr_reset.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class Int 
{
public:
   Int( int i ) 
   {
      x = i;
      cout << "Constructing " << ( void* )this << " Value: " << x << endl; 
   };
   ~Int( ) 
   {
      cout << "Destructing " << ( void* )this << " Value: " << x << endl; 
   };

   int x;
};

int main( ) 
{
   auto_ptr<Int> pi ( new Int( 5 ) );
   pi.reset( new Int( 6 ) );
   Int* pi2 = pi.get ( );
   Int* pi3 = pi.release ( );
   if ( pi2 == pi3 )
      cout << "pi2 == pi3" << endl;
   delete pi3;
}
  

需求

標題: <memory>

命名空間: std

請參閱

參考

auto_ptr Class