Share via


auto_ptr 클래스

제어가 블록을 나갈 때 리소스가 자동으로 제거되도록 리소스를 스마트 포인터로 래핑합니다.

C++11 unique_ptrauto_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 형식의 개체에 대한 생성자입니다.

Typedef

이름 설명
element_type 이 형식은 템플릿 매개 변수 Type의 동의어입니다.

함수

속성 설명
get 이 멤버 함수는 저장된 포인터 myptr을 반환합니다.
release 이 멤버는 저장된 포인터 myptr을 null 포인터로 대체하고 이전에 저장된 포인터를 반환합니다.
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 개체입니다.

설명

첫 번째 생성자는 할당된 개체에 저장된 포인터를 저장 ptrmyptr합니다. 두 번째 생성자는 저장하여 저장된 right포인터의 소유권을 전송합니다 right. myptr에서 릴리스.

세 번째 생성자는 저장 right된다는 점을 제외하고 두 번째 생성자와 동일하게 동작합니다. ref. release에 저장myptr되는 참조는 i0>입니다right.

포인터를 포인터로 암시적으로 변환할 Other 수 있는 경우 템플릿 생성자는 두 번째 생성자와 동일하게 동작합니다 Type.

예시

// 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();

Return Value

저장된 포인터 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 형식의 개체입니다.

Return Value

auto_ptr<Type> 형식의 개체에 대한 참조입니다.

설명

할당은 식을 delete myptr평가하지만 저장된 포인터 myptr 가 할당 결과로 변경되는 경우에만 계산됩니다. 그런 다음 오른쪽에 저장하여 오른쪽저장된 포인터의 소유권을 전송합니다.myptr에서 release 사용 함수에서 *this을 반환합니다.

예시

멤버 연산자 사용의 예는 다음을 참조하세요 auto_ptr.

operator*

auto_ptr 형식의 개체에 대한 역참조 연산자입니다.

Type& operator*() const throw();

Return Value

포인터가 소유하는 형식 Type 의 개체에 대한 참조입니다.

설명

간접 연산자는 *get을 반환합니다. 따라서 저장된 포인터는 null이 아니어야 합니다.

예시

멤버 함수를 사용하는 방법의 예는 다음을 참조하세요 auto_ptr.

operator->

멤버 액세스를 허용하기 위한 연산자입니다.

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

Return Value

소유하는 개체의 멤버입니다 auto_ptr .

설명

선택 연산get( )자는 식>apmember이 ( 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();

Return Value

형식 캐스트 연산자는 Other>(*this)를 반환합니다auto_ptr<.

예시

// 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();

Return Value

형식 캐스트 연산자는 auto_ptr_ref>(*this)Other< 반환합니다.

예시

// 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

이 멤버는 저장된 포인터 myptr을 null 포인터로 대체하고 이전에 저장된 포인터를 반환합니다.

Type *release() throw();

Return Value

이전에 저장된 포인터입니다.

설명

이 멤버는 저장된 포인터 myptr을 null 포인터로 대체하고 이전에 저장된 포인터를 반환합니다.

예시

// 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