共用方式為


auto_ptr 類別

以智慧型指標包裝資源,確保當控制離開區塊時,會自動終結該資源。

從 C++11 開始,請使用 unique_ptr 而不是 auto_ptr。 如需詳細資訊,請參閱 unique_ptr 類別auto_ptr 在 C++11 中已被取代,並在 C++17 中移除。

如需 throw() 和例外狀況處理的詳細資訊,請參閱例外狀況規格 (throw)

語法

class auto_ptr {
    typedef Type element_type;
    explicit auto_ptr(Type* ptr = 0) throw();
    auto_ptr(auto_ptr<Type>& right) throw()
        ;
    template <class Other>
    operator auto_ptr<Other>() throw();
    template <class Other>
    auto_ptr<Type>& operator=(auto_ptr<Other>& right) throw();
    template <class Other>
    auto_ptr(auto_ptr<Other>& right);
    auto_ptr<Type>& operator=(auto_ptr<Type>& right);
    ~auto_ptr();
    Type& operator*() const throw();
    Type * operator->()const throw();
    Type *get() const throw();
    Type *release()throw();
    void reset(Type* ptr = 0);
};

參數

right
要從中取得現有資源的 auto_ptr

ptr
此指標指定了要取代的已儲存指標。

備註

類別範本描述設定物件的智慧型指標,稱為 auto_ptr。 這個指標必須是 null,或指定 new 所配置的物件。 如果將 auto_ptr 的儲存值指派給另一個物件,則會轉移擁有權。 (它會以 Null 指標取代傳輸之後的預存值。的解構函式 auto_ptr<Type> 會刪除已配置的物件。 auto_ptr<Type> 可確保當控制離開區塊時 (即使是透過擲回例外狀況),會自動刪除配置物件。 您不應該建構擁有相同物件的兩個 auto_ptr<Type> 物件。

您可以將 auto_ptr<Type> 物件當做函式呼叫的引數,以傳值方式來傳遞。 auto_ptr不能是任何標準連結庫容器的專案。 您無法使用 C++ 標準連結庫容器可靠地管理一連串 auto_ptr<Type> 的物件。

成員

建構函式

名稱 描述
auto_ptr auto_ptr 類型物件的建構函式。

Typedefs

名稱 描述
element_type 此類型是範本參數 Type的同義字。

函式

名稱 描述
get 這個成員函式會傳回儲存的指標 myptr
release 這個成員會以 null 指標取代儲存的指標 myptr,並傳回先前儲存的指標。
reset 這個成員函式只有在儲存的指標值 myptr 因函式呼叫而變更時,才會評估運算式 delete myptr。 然後會以 ptr 取代儲存的指標。

操作員

名稱 描述
operator= 指派運算子,將擁有權從一個 auto_ptr 物件轉移至另一個物件。
operator* auto_ptr 類型物件的取值運算子。
operator-> 允許成員存取的運算子。
operator auto_ptr<Other> 從一種 auto_ptr 轉換成另一種 auto_ptr
operator auto_ptr_ref<Other> auto_ptr 轉換成 auto_ptr_ref

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

備註

第一個建構函式會 ptr 儲存在 中 myptr,這是所配置物件的預存指標。 第二個建構函式會儲存 right,以轉移 right 中所儲存指標的擁有權。 在中myptr發行

第三個建構函式的行為與第二個建構函式相同,不同之處在於它會儲存 rightref. release 在 中 myptr,其中 ref 是儲存在 中的 right參考。

如果的指標可以隱含轉換成 的Type指標Other,則樣板建構函式的行為與第二個建構函式相同。

範例

// 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;
}
Constructing 00311AF8
5
7
Destructing 00311AF8

element_type

此類型是範本參數 Type的同義字。

typedef Type element  _type;

get

這個成員函式會傳回儲存的指標 myptr

Type *get() const throw();

傳回值

儲存的指標 myptr

範例

// auto_ptr_get.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;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

operator=

指派運算子,將擁有權從一個 auto_ptr 物件轉移至另一個物件。

template <class Other>
    auto_ptr<Type>& operator=(auto_ptr<Other>& right) throw();
auto_ptr<Type>& operator=(auto_ptr<Type>& right) throw();
auto_ptr<Type>& operator=(auto_ptr_ref<Type> right) throw();

參數

right
auto_ptr 類型的物件。

傳回值

類型為 auto_ptr<Type> 之物件的參考。

備註

指派會評估表達式 delete myptr,但只有在儲存的指標 myptr 因指派而變更時。 然後,它會藉由儲存許可權來轉移儲存在右側的指標擁有權。myptr 中的 release。 函式會傳回 *this

範例

如需使用成員運算子的範例,請參閱 auto_ptr

operator*

auto_ptr 類型物件的取值運算子。

Type& operator*() const throw();

傳回值

指標擁有之型 Type 別對象的參考。

備註

間接取值運算子會傳回 *get。 因此,儲存的指標不得為 Null。

範例

如需如何使用成員函式的範例,請參閱 auto_ptr

operator->

允許成員存取的運算子。

Type * operator->() const throw();

傳回值

擁有的物件 auto_ptr 成員。

備註

選取運算符會傳 get( )回 ,讓表達式 ap->member 的行為與 ( ap相同。 get() -->member,其中 ap 是 類別>auto_ptr<Type的物件。 因此,預存指標不得為 Null,而且 Type 必須是具有成員的 member 類別、結構或等位型別。

範例

如需如何使用成員函式的範例,請參閱 auto_ptr

operator auto_ptr<Other>

從一種 auto_ptr 轉換成另一種 auto_ptr

template <class Other>
operator auto _ptr<Other>() throw();

傳回值

類型轉換運算符會傳auto_ptr<回 Other>(*this)

範例

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

using namespace std;
int main()
{
   auto_ptr<int> pi ( new int( 5 ) );
   auto_ptr<const int> pc = ( auto_ptr<const int> )pi;
}

operator auto_ptr_ref<Other>

auto_ptr 轉換成 auto_ptr_ref

template <class Other>
operator auto _ptr  _ref<Other>() throw();

傳回值

類型轉換運算符會傳回auto_ptr_refOther>(*this)<

範例

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

using namespace std;

class C {
public:
    C(int _i) : m_i(_i) {
    }
    ~C() {
        cout << "~C:  " << m_i << "\n";
    }
    C &operator =(const int &x) {
        m_i = x;
        return *this;
    }
    int m_i;
};
void f(auto_ptr<C> arg) {
};
int main()
{
    const auto_ptr<C> ciap(new C(1));
    auto_ptr<C> iap(new C(2));

    // Error: this implies transfer of ownership of iap's pointer
    // f(ciap);
    f(iap); // compiles, but gives up ownership of pointer

            // here, iap owns a destroyed pointer so the following is bad:
            // *iap = 5; // BOOM

    cout << "main exiting\n";
}
~C:  2
main exiting
~C:  1

release

這個成員會以 null 指標取代儲存的指標 myptr,並傳回先前儲存的指標。

Type *release() throw();

傳回值

之前儲存的指標。

備註

這個成員會以 null 指標取代儲存的指標 myptr,並傳回先前儲存的指標。

範例

// auto_ptr_release.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;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

reset

成員函式會評估表達式 delete myptr,但只有在預存指標值 myptr 因函數調用而變更時。 然後會以 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;
}
Constructing 00311AF8 Value: 5
Constructing 00311B88 Value: 6
Destructing 00311AF8 Value: 5
pi2 == pi3
Destructing 00311B88 Value: 6

另請參閱

unique_ptr Class