다음을 통해 공유


CTypedPtrList Class

Provides a type-safe "wrapper" for objects of class CPtrList.

template< class BASE_CLASS, class TYPE >
class CTypedPtrList : public BASE_CLASS

Parameters

  • BASE_CLASS
    Base class of the typed pointer list class; must be a pointer list class (CObList or CPtrList).

  • TYPE
    Type of the elements stored in the base-class list.

Members

Public Methods

Name

Description

CTypedPtrList::AddHead

Adds an element (or all the elements in another list) to the head of the list (makes a new head).

CTypedPtrList::AddTail

Adds an element (or all the elements in another list) to the tail of the list (makes a new tail).

CTypedPtrList::GetAt

Gets the element at a given position.

CTypedPtrList::GetHead

Returns the head element of the list (cannot be empty).

CTypedPtrList::GetNext

Gets the next element for iterating.

CTypedPtrList::GetPrev

Gets the previous element for iterating.

CTypedPtrList::GetTail

Returns the tail element of the list (cannot be empty).

CTypedPtrList::RemoveHead

Removes the element from the head of the list.

CTypedPtrList::RemoveTail

Removes the element from the tail of the list.

CTypedPtrList::SetAt

Sets the element at a given position.

Remarks

When you use CTypedPtrList rather than CObList or CPtrList, the C++ type-checking facility helps eliminate errors caused by mismatched pointer types.

In addition, the CTypedPtrList wrapper performs much of the casting that would be required if you used CObList or CPtrList.

Because all CTypedPtrList functions are inline, use of this template does not significantly affect the size or speed of your code.

Lists derived from CObList can be serialized, but those derived from CPtrList cannot.

When a CTypedPtrList object is deleted, or when its elements are removed, only the pointers are removed, not the entities they reference.

For more information on using CTypedPtrList, see the articles Collections and Template-Based Classes.

Example

This example creates an instance of CTypedPtrList, adds one object, serializes the list to disk, and then deletes the object:

typedef CTypedPtrList<CObList, CMyObject*>  CMyList;
CMyList ml;
CMyObject* pMyObject = new CMyObject();
ml.AddTail(pMyObject);

CFileException e;
CFile myFile; 
myFile.Open(_T("CTypedPtrList_File.txt"), 
   CFile::modeCreate|CFile::modeWrite, &e);
CArchive ar(&myFile, CArchive::store);
ml.Serialize(ar);

ar.Close();
myFile.Close(); 

while (!ml.IsEmpty())
{
   delete ml.GetHead();
   ml.RemoveHead();
}
class CMyObject : public CObject
{
public:
     int i;
     void Serialize(CArchive& ar);
     CMyObject() { i = 9876;}
protected:
     DECLARE_SERIAL(CMyObject)
};

IMPLEMENT_SERIAL(CMyObject, CObject, 0)  

void CMyObject::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);
    if(ar.IsStoring())
         ar << i;
    else
         ar >> i;
}

Inheritance Hierarchy

BASE_CLASS

_CTypedPtrList

CTypedPtrList

Requirements

Header: afxtempl.h

See Also

Reference

Hierarchy Chart

CPtrList Class

CObList Class

Concepts

MFC Sample COLLECT