Let's try one base on one of Microsoft's examples. Most of the code for what you want to do goes into a header file:
#ifdef _AFX_ENABLE_INLINES
#define _AFX_INLINE AFX_INLINE
#if !defined(_AFX_CORE_IMPL) || !defined(_AFXDLL) || defined(_DEBUG)
#define _AFX_PUBLIC_INLINE AFX_INLINE
#else
#define _AFX_PUBLIC_INLINE
#endif
#endif
template<class TYPE, class ARG_TYPE>
class CCuteBundle;
Then below the code above, bases upon the CArray class, in the same file you might do the following:
template<class TYPE, class ARG_TYPE = const TYPE>
class CCuteBundle : public CObject
{
public:
CCuteBundle();
INT_PTR GetSize() const;
INT_PTR GetCount() const;
INT_PTR GetUpperBound() const;
BOOL IsEmpty() const;
void SortMePlease(INT_PTR nIndex, ARG_TYPE sortElement);
TYPE& GetAt(INT_PTR nIndex);
void SetAt(INT_PTR nIndex, ARG_TYPE newElement);
TYPE& ElementAt(INT_PTR nIndex);
const TYPE& operator[](INT_PTR nIndex) const;
TYPE& operator[](INT_PTR nIndex);
protected:
TYPE* m_pData; INT_PTR m_nSize;
};
So OK so far. Then You need to Actually Implement the functions. The implementation exists with in the same header file:
template<class TYPE, class ARG_TYPE>
AFX_INLINE INT_PTR CCuteBundle<TYPE, ARG_TYPE>::GetSize() const
{ return m_nSize; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE INT_PTR CCuteBundle<TYPE, ARG_TYPE>::GetCount() const
{ return m_nSize; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE BOOL CCuteBundle<TYPE, ARG_TYPE>::IsEmpty() const
{ return m_nSize == 0; }
AFX_INLINE INT_PTR CCuteBundle<TYPE, ARG_TYPE>::GetUpperBound() const
{ return m_nSize-1; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& CCuteBundle<TYPE, ARG_TYPE>::GetAt(INT_PTR nIndex)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
if(nIndex >= 0 && nIndex < m_nSize)
return m_pData[nIndex];
AfxThrowInvalidArgException();
}
template<class TYPE, class ARG_TYPE>
AFX_INLINE void CCuteBundle<TYPE, ARG_TYPE>::SetAt(INT_PTR nIndex, ARG_TYPE newElement)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
if(nIndex >= 0 && nIndex < m_nSize)
m_pData[nIndex] = newElement;
else
AfxThrowInvalidArgException();
}
template<class TYPE, class ARG_TYPE>
AFX_INLINE const TYPE& CCuteBundle<TYPE, ARG_TYPE>::operator[](INT_PTR nIndex) const
{ return GetAt(nIndex); }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& CCuteBundle<TYPE, ARG_TYPE>::operator[](INT_PTR nIndex)
{ return ElementAt(nIndex); }
So now in the sections below, are using the functions just defined above, so ground work is key to your implementation.
template<class TYPE, class ARG_TYPE>
void CCuteBundle<type,>::SortMePlease(INT_PTR nIndex, ARG_TYPE sortElement)
{
ASSERT_VALID(this);
ASSERT(sortElement != NULL);
ASSERT(nIndex > 0);
if(m_pData[nIndex - 1] > sortElement)
{
m_pData[nIndex] = m_pData[nIndex - 1];
}
else
{
m_pData[nIndex] = sortElement;
}
}
You may want to add some of the other CArray type functions just in case ...
Then use your new class in your Main Program.