I made another class. I know there is a similar class in boost, but it is very touchy on syntax. This one I hope is a little better than my other, but I thought have a smart pointer class for array's would be kinda cool.
so here it is, and hopefully you guys catch what I have not ( which is very likely )
Any input is greatly appreciated.Code:namespace Varia { template<typename T> void Free(T*& p_Obj) { delete p_Obj; p_Obj = 0; } template<typename T> void FreeArray(T*& p_Obj) { delete [] p_Obj; p_Obj = 0; } template<typename T> class Array { private: T* m_Array; unsigned m_Size; public: Array() :m_Array(0), m_Size(0) { } Array(unsigned p_Size) :m_Array(0), m_Size(p_Size) { if ( m_Size > 0 ) m_Array = new T[m_Size]; } Array(const Array& p_Copy) :m_Array(0), m_Size(0) { Copy(p_Copy); } const Array& operator=(const Array& p_Copy) { Copy(p_Copy); return *this; } void Realloc(unsigned p_Size) { T* TempArray = 0; if ( p_Size+1 < 1 ) return; TempArray = new T[p_Size+1]; for ( unsigned x = 0; x < p_Size+1 && x < m_Size; ++x ) { TempArray[x] = m_Array[x]; } Release(); m_Array = TempArray; m_Size = p_Size+1; } void Release() { FreeArray(m_Array); } T& operator[](unsigned p_Index) { if ( p_Index >= m_Size ) { Realloc(p_Index); } return m_Array[p_Index]; } const T& operator[](unsigned p_Index) const { if ( p_Index >= m_Size) { Realloc(p_Index); } return m_Array[p_Index]; } private: void Copy(const Array& p_Copy) { if ( &p_Copy == this ) return; T* TempArray = 0; if ( p_Copy.m_Size > 0 ) { TempArray = new T[p_Copy.m_Size]; for ( unsigned x = 0; x < p_Copy.m_Size; ++x ) { TempArray[x] = p_Copy.m_Array[x]; } } Release(); m_Array = TempArray; m_Size = p_Copy.m_Size; } }; }
- Main purpose, I am getting tired of forgetting to clean up dynamic memory.