다음을 통해 공유


CAutoPtr Class

이 클래스는 스마트 포인터가 개체를 나타냅니다.

중요

런타임에서 Windows를 실행 하는 응용 프로그램에서이 클래스와 해당 멤버를 사용할 수 없습니다.

template< 
typename T 
> 
class CAutoPtr

매개 변수

  • T
    포인터 형식입니다.

Members

Public 생성자

이름

설명

CAutoPtr::CAutoPtr

생성자입니다.

CAutoPtr::~CAutoPtr

소멸자

Public 메서드

이름

설명

CAutoPtr::Attach

기존 포인터의 소유권을 가져오려면이 메서드를 호출 합니다.

CAutoPtr::Detach

소유권에 대 한 포인터를 해제 하려면이 메서드를 호출 합니다.

CAutoPtr::Free

가리키는 개체를 삭제 하려면이 메서드를 호출 하는 CAutoPtr.

Public 연산자

이름

설명

CAutoPtr::operator T*

캐스트 연산자입니다.

CAutoPtr::operator =

할당 연산자입니다.

CAutoPtr::operator ->

포인터 멤버 연산자입니다.

공용 데이터 멤버

이름

설명

CAutoPtr::m_p

포인터 데이터 멤버 변수입니다.

설명

이 클래스 만들기 및 관리 범위를 벗어난 작아지면 리소스를 자동으로 해제 하 여 메모리 누수를 방지 하는 데 도움이 되는 스마트 포인터에 대 한 메서드를 제공 합니다.

추가로, CAutoPtr의 복사 생성자와 할당 연산자 소유권의 포인터가 원본 포인터 포인터를 대상 복사 및 원본 포인터를 NULL로 설정 합니다. 따라서 둘 수 없는 CAutoPtr 각각 동일한 포인터를 저장 하는 개체 및이 동일한 포인터를 두 번 삭제의 가능성을 줄여줍니다.

CAutoPtr또한 포인터 컬렉션 만들기를 단순화합니다. 컬렉션 클래스를 파생 및 소멸자를 재정의 하는 대신 컬렉션을 확인 하는 것입니다 CAutoPtr 개체입니다. 컬렉션이 삭제 될 때의 CAutoPtr 개체가 범위를 벗어나면 서 자신을 자동으로 삭제 합니다.

CHeapPtr 및 변형 작업을 동일한 방식으로 CAutoPtr, 할당 하 고 다른 힙 함수 대신 C++를 사용 하 여 메모리를 확보 된다는 점을 제외 하 고 삭제 연산자. CAutoVectorPtr 유사 합니다 CAutoPtr, 사용 하도록 하는 유일한 차이점은 벡터 new벡터 delete 할당 하 고 메모리를 해제 합니다.

참고 CAutoPtrArrayCAutoPtrList 배열 또는 목록 스마트 포인터의 경우 필요 합니다.

요구 사항

헤더: atlbase.h

예제

// A simple class for demonstration purposes 

class MyClass 
{
   int iA;
   int iB;
public:
   MyClass(int a, int b);
   void Test();
};

MyClass::MyClass(int a, int b)
{
   iA = a;
   iB = b;
}

void MyClass::Test()
{
   ATLASSERT(iA == iB);
}

// A simple function 

void MyFunction(MyClass* c)
{
   c->Test();
}

int UseMyClass()
{
   // Create an object of MyClass.
   MyClass *pMyC = new MyClass(1, 1);

   // Create a CAutoPtr object and have it take 
   // over the pMyC pointer by calling Attach.
   CAutoPtr<MyClass> apMyC;
   apMyC.Attach(pMyC);

   // The overloaded -> operator allows the  
   // CAutoPtr object to be used in place of the pointer.
   apMyC->Test();

   // Assign a second CAutoPtr, using the = operator.
   CAutoPtr<MyClass> apMyC2;
   apMyC2 = apMyC;

   // The casting operator allows the 
   // object to be used in place of the pointer.
   MyFunction(pMyC);
   MyFunction(apMyC2);

   // Detach breaks the association, so after this 
   // call, pMyC is controlled only by apMyC.
   apMyC2.Detach();

   // CAutoPtr destroys any object it controls when it 
   // goes out of scope, so apMyC destroys the object  
   // pointed to by pMyC here. 
   return 0;
}

참고 항목

참조

CHeapPtr Class

CAutoVectorPtr Class

기타 리소스

ATL Class Overview