Click here to Skip to main content
15,881,791 members
Articles / Desktop Programming / MFC

Quick and Dirty Collection Class Notes

Rate me:
Please Sign up or sign in to vote.
4.11/5 (10 votes)
28 Nov 2000 96.9K   1K   33  
An article describing MFC Collection Classes
Expand the Header Files folder, and open file StdAfx.h.
At the end of the file, type:
#include <afxtempl.h>    // MFC templates

///////////////////////////////////////////////////////////////////////////////
CArray < class TYPE, class ARG_TYPE >

    TYPE        Template parameter specifying the type of objects stored in the array.
                TYPE is a parameter that is returned by CArray.

    ARG_TYPE    Template parameter specifying the argument type used to access
                objects stored in the array. Often a reference to TYPE.
                ARG_TYPE is a parameter that is passed to CArray.

    The CArray class supports arrays that are are similar to C arrays, but can
    dynamically shrink and grow as necessary


    ==================================================
    Class Members
    ==================================================
    Add
        int Add( ARG_TYPE newElement );
            throw( CMemoryException );
    Append
        int Append( const CArray& src );
    Copy
        void Copy( const CArray& src );
    ElementAt
        TYPE& ElementAt( int nIndex );
    FreeExtra
        void FreeExtra( );
    GetAt
        TYPE GetAt( int nIndex ) const;
    GetData
        const TYPE* GetData( ) const;
    GetSize
        int GetSize( ) const;
    GetUpperBound
        int GetUpperBound( ) const;
    InsertAt
        void InsertAt( int nIndex, ARG_TYPE newElement, int nCount = 1 );
            throw( CMemoryException );
        void InsertAt( int nStartIndex, CArray* pNewArray );
            throw( CMemoryException );
                [pNewArray - Another array that contains elements
                 to be added to this array.]
    RemoveAll
        void RemoveAll( );
    RemoveAt
        void RemoveAt( int nIndex, int nCount = 1 );
    SetAt
        void SetAt( int nIndex, ARG_TYPE newElement );
    SetAtGrow
        void SetAtGrow( int nIndex, ARG_TYPE newElement );
            throw( CMemoryException );
    SetSize
        void SetSize( int nNewSize, int nGrowBy = -1 );
            throw( CMemoryException );
    operator[]
        TYPE& operator []( int nIndex );
        TYPE operator []( int nIndex ) const;

    ==================================================
    Create
    ==================================================
    CArray<CPoint,CPoint> m_ObjectArray;
    m_ObjectArray.SetSize(number_of_elements, elements_to_allocate_if_increased);

    CArray<CPerson*, CPerson*> m_PointerArray;
    //Constructs an empty array. The array grows one element at a time


    ==================================================
    Insert
    ==================================================
    CPoint pt(10,10);
    m_ObjectArray.Add(pt);

    ==================================================
    Iterate
    ==================================================
    CArray<CPoint,CPoint>&ptArray = m_ObjectArray;
	for (int nIndex = 0; nIndex < ptArray.GetSize(); nIndex++)
	{
		CPoint pt = ptArray[nIndex];
    }

    ==================================================
    Find
    ==================================================

    ==================================================
    Update
    ==================================================
    CPoint pt(m_x, m_y);
    m_ObjectArray[nSel] = pt;

    ==================================================
    Delete
    ==================================================
    m_ObjectArray.RemoveAt(nSel);
    m_ObjectArray.RemoveAll();

    int i = 0;
    while (i < m_PointerArray.GetSize() )
    {
        delete m_PointerArray.GetAt( i++ );
    }
    m_PointerArray.RemoveAll();

    ==================================================
    Serialize
    ==================================================
    m_ObjectArray.Serialize( ar );







By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions