CTypedPtrList Class
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

CTypedPtrList Class 

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

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



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


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

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.

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

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


while (!ml.IsEmpty())
   delete ml.GetHead();

//where CMyObject is defined by the following files:

class CMyObject : public CObject
     int i;
     void Serialize(CArchive& ar);
     CMyObject() { i = 9876;}

#include "stdafx.h"
#include "CMyObject.h"

IMPLEMENT_SERIAL(CMyObject, CObject, 0)  

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

Header: afxtempl.h

© 2016 Microsoft