auto_ptr sınıfı
Akıllı işaretçiyi bir kaynağın çevresinde kaydırarak denetim bir blok bıraktığında kaynağın otomatik olarak yok edilmesini sağlar.
C++11'den başlayarak yerine auto_ptr
kullanınunique_ptr
. Daha fazla bilgi için bkz unique_ptr
. sınıf. auto_ptr
C++11'de kullanım dışı bırakıldı ve C++17'de kaldırıldı.
ve özel durum işleme hakkında throw()
daha fazla bilgi için bkz . Özel Durum Belirtimleri (throw).
Sözdizimi
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);
};
Parametreler
right
auto_ptr
Mevcut bir kaynağın alındığı kaynak.
ptr
Depolanan işaretçiyi değiştirmek için belirtilen işaretçi.
Açıklamalar
Sınıf şablonu, ayrılmış bir nesneye yönelik olarak adlandırılan auto_ptr
akıllı işaretçiyi açıklar. İşaretçi null olmalıdır veya tarafından new
ayrılmış bir nesne belirtmelidir. Depolanan auto_ptr
değeri başka bir nesneye atanmışsa sahipliği aktarır. (Aktarımdan sonra depolanan değeri null işaretçiyle değiştirir.) için auto_ptr<Type>
yıkıcı, ayrılan nesneyi siler. , auto_ptr<Type>
ayrılmış bir nesnenin, denetim bir bloktan ayrıldığında( hatta bir özel durum aracılığıyla) otomatik olarak silinmesini sağlar. Aynı nesneye sahip iki auto_ptr<Type>
nesne oluşturmamalısınız.
Bir auto_ptr<Type>
nesneyi bir işlev çağrısına bağımsız değişken olarak değere göre geçirebilirsiniz. Herhangi auto_ptr
bir Standart Kitaplık kapsayıcısının öğesi olamaz. C++ Standart Kitaplık kapsayıcısı ile bir nesne dizisini auto_ptr<Type>
güvenilir bir şekilde yönetemezsiniz.
Üyeler
Oluşturucular
Adı | Açıklama |
---|---|
auto_ptr |
türündeki auto_ptr nesneler için oluşturucu. |
Tür tanımları
Adı | Açıklama |
---|---|
element_type |
türü, şablon parametresi Type için bir eş anlamlıdır. |
İşlevler
Adı | Açıklama |
---|---|
get |
Üye işlevi depolanan işaretçiyi myptr döndürür. |
release |
Üye, depolanan işaretçiyi null işaretçiyle myptr değiştirir ve daha önce depolanmış işaretçiyi döndürür. |
reset |
Üye işlevi ifadesini delete myptr değerlendirir, ancak yalnızca depolanan işaretçi değeri myptr işlev çağrısının sonucu olarak değişirse. Ardından depolanan işaretçiyi ile ptr değiştirir. |
İşleçler
Adı | Açıklama |
---|---|
operator= |
Sahipliği bir nesneden diğerine auto_ptr aktaran atama işleci. |
operator* |
türündeki auto_ptr nesneler için başvuru kaldırma işleci. |
operator-> |
Üye erişimine izin verme işleci. |
operator auto_ptr<Other> |
Bir türden başka bir auto_ptr türe auto_ptr yayınlar. |
operator auto_ptr_ref<Other> |
bir 'den öğesine auto_ptr yayınlar auto_ptr_ref . |
auto_ptr
türündeki auto_ptr
nesneler için oluşturucu.
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();
Parametreler
ptr
Kapsülleyen auto_ptr
nesnenin işaretçisi.
right
auto_ptr
Oluşturucu tarafından kopyalanacak nesne.
Açıklamalar
İlk oluşturucu, ayrılmış nesnenin depolanan işaretçisini içinde myptr
depolarptr
. İkinci oluşturucu, içinde right
depolanan işaretçinin sahipliğini depolayarak right
aktarır. sürümünde yayın.myptr
Üçüncü oluşturucu, depolar right
dışında ikinciyle aynı şekilde davranır. ref
. release
içinde myptr
, burada ref
içinde depolanan right
başvurudur.
şablon oluşturucu, işaretçi örtük olarak işaretçiye Other
dönüştürülebiliyorsa Type
ikinci oluşturucuyla aynı şekilde davranır.
Örnek
// 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
türü, şablon parametresi Type
için bir eş anlamlıdır.
typedef Type element _type;
get
Üye işlevi depolanan işaretçiyi myptr
döndürür.
Type *get() const throw();
İade Değeri
Depolanan işaretçi myptr
.
Örnek
// 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=
Sahipliği bir nesneden diğerine auto_ptr
aktaran atama işleci.
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();
Parametreler
right
auto_ptr
türünün bir nesnesi.
İade Değeri
türünde auto_ptr<Type>
bir nesneye başvuru.
Açıklamalar
Atama ifadesini delete myptr
değerlendirir, ancak yalnızca depolanan işaretçi myptr
atamanın sonucu olarak değişirse. Ardından sağa depolayarak sağda depolanan işaretçinin sahipliğini aktarır.release
içinde .myptr
işlevi döndürür *this
.
Örnek
Üye işlecinin kullanımına ilişkin bir örnek için bkz auto_ptr
. .
operator*
türündeki auto_ptr
nesneler için başvuru kaldırma işleci.
Type& operator*() const throw();
İade Değeri
İşaretçinin sahip olduğu türdeki Type
bir nesneye başvuru.
Açıklamalar
Dolaylı işleci döndürür *
get
. Bu nedenle, depolanan işaretçi null olmamalıdır.
Örnek
Üye işlevinin nasıl kullanılacağına ilişkin bir örnek için bkz auto_ptr
. .
operator->
Üye erişimine izin verme işleci.
Type * operator->() const throw();
İade Değeri
Sahibi olan nesnenin auto_ptr
bir üyesi.
Açıklamalar
Seçim işleci , ifadesinin ap
>member
( ap
ile aynı şekilde davranmasını) sağlar.get
( )
get
() )->member
, burada ap
sınıfının>auto_ptr<
Type
bir nesnesidir. Bu nedenle, depolanan işaretçi null olmamalıdır ve Type
bir üye ile member
bir sınıf, yapı veya birleşim türü olmalıdır.
Örnek
Üye işlevinin nasıl kullanılacağına ilişkin bir örnek için bkz auto_ptr
. .
operator auto_ptr<Other>
Bir türden başka bir auto_ptr
türe auto_ptr
yayınlar.
template <class Other>
operator auto _ptr<Other>() throw();
İade Değeri
Tür atama işleci Diğer>(*this)
döndürürauto_ptr<
.
Örnek
// 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>
bir 'den öğesine auto_ptr
yayınlar auto_ptr_ref
.
template <class Other>
operator auto _ptr _ref<Other>() throw();
İade Değeri
Tür atama işleci auto_ptr_ref>(*this)
Other
<
döndürür.
Örnek
// 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
Üye, depolanan işaretçiyi null işaretçiyle myptr
değiştirir ve daha önce depolanmış işaretçiyi döndürür.
Type *release() throw();
İade Değeri
Önceden depolanan işaretçi.
Açıklamalar
Üye, depolanan işaretçiyi null işaretçiyle myptr
değiştirir ve daha önce depolanmış işaretçiyi döndürür.
Örnek
// 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
Üye işlevi ifadesini delete myptr
değerlendirir, ancak yalnızca bir işlev çağrısının sonucu olarak depolanan işaretçi değeri myptr
değişirse. Ardından depolanan işaretçiyi ile ptr
değiştirir.
void reset(Type* ptr = 0);
Parametreler
ptr
Depolanan işaretçiyi değiştirmek için belirtilen işaretçi myptr
.
Örnek
// 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
Ayrıca bkz.
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin