Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
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_ptrkullanı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_ptrakıllı işaretçiyi açıklar. İşaretçi null olmalıdır veya tarafından newayrı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
| Veri Akışı Adı | Açıklama |
|---|---|
auto_ptr |
türündeki auto_ptrnesneler için oluşturucu. |
Tür tanımları
| Veri Akışı Adı | Açıklama |
|---|---|
element_type |
türü, şablon parametresi Typeiçin bir eş anlamlıdır. |
İşlevler
| Veri Akışı Adı | Açıklama |
|---|---|
get |
Üye işlevi depolanan işaretçiyi myptrdö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 myptrdeğ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 ptrdeğiştirir. |
İşleçler
| Veri Akışı Adı | Açıklama |
|---|---|
operator= |
Sahipliği bir nesneden diğerine auto_ptr aktaran atama işleci. |
operator* |
türündeki auto_ptrnesneler 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_ptryayınlar. |
operator auto_ptr_ref<Other> |
bir 'den öğesine auto_ptr yayınlar auto_ptr_ref. |
auto_ptr
türündeki auto_ptrnesneler 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 myptrdepolarptr. İkinci oluşturucu, içinde rightdepolanan işaretçinin sahipliğini depolayarak rightaktarır. sürümünde yayın. myptr
Üçüncü oluşturucu, depolar rightdışında ikinciyle aynı şekilde davranır. ref. release içinde myptr, burada ref içinde depolanan rightbaşvurudur.
şablon oluşturucu, işaretçi örtük olarak işaretçiye Other dönüştürülebiliyorsa Typeikinci 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 Typeiçin bir eş anlamlıdır.
typedef Type element _type;
get
Üye işlevi depolanan işaretçiyi myptrdöndürür.
Type *get() const throw();
Dönüş 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.
Dönüş Değeri
türünde auto_ptr<Type>bir nesneye başvuru.
Açıklamalar
Atama ifadesini delete myptrdeğ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.releaseiç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_ptrnesneler için başvuru kaldırma işleci.
Type& operator*() const throw();
Dönüş 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();
Dönüş Değeri
Sahibi olan nesnenin auto_ptr bir üyesi.
Açıklamalar
Seçim işleci , ifadesinin ap>member ( apile 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_ptryayınlar.
template <class Other>
operator auto _ptr<Other>() throw();
Dönüş 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();
Dönüş 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();
Dönüş 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 myptrdeğ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 ptrdeğ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