. Home Feedback Contents Search

CArray 

Up Next

The CArray template encapsulates array functionality. Addressing individual items is accomplished in the normal array fashion. The very best thing about the CArray is that it can expand to meet its memory needs. The only significant downside is that it doesn't delete encapsulated pointers so the programmer must take care of this little chore himself.

Here's a little example of how to load it and release it. The collection hangs on to an array of pointers to CCell objects.

In the H file

#include "Cell.h"

CArray<CCell*, CCell*> m_arCells;

In the CPP file

CCellRow::CCellRow(void)
    {
    for ( int x=0; x<9; x++ )
        m_arCells.Add(
new CCell() );
    }

CCellRow::~CCellRow(
void)
    {
   
for ( int x=0; x<m_arCells.GetCount(); x++ )
        {
        CCell* pCell = m_arCells[x];
       
if ( pCell )
            {
           
delete pCell;
            m_arCells[x] = 0;
            }
        m_arCells.RemoveAll();
        }
    }

Up Next