I use a function called adjust pointer which just allocates a new array, copies the old one into it and deletes the old one.
template <class T> void cGhost :: AdjustPointer(T *&Var, int &Size, int Increment){ if (Increment < 0) { if (Size + Increment < 1) { if (Var == NULL) return; else { SAFE_DELETE_ARRAY(Var); Size = 0; return; } } } T *New = new T[Size + Increment]; memcpy(&New[0], &Var[0], sizeof(T) * Size); SAFE_DELETE_ARRAY(Var); Var = New; Size += Increment;}
But I am running into problems with strings. It doesn't seem to copy properly and I ended up having to use a for loop and set each iteration directly. I'm guessing it has something to do with the how the string stores it array of chars. Maybe a copy constructor is needed with strings? I'm not sure. I've spent a long time looking up this problem and I can't seem to find any solution.
Here's the array class for reference.
#pragma once#include "GhostVars.h"template<class T>class cArray{public: cArray(void) { Array = NULL; Size = 0; Increment = 1; Last = 0; CurIndex = 0; AutoDelete = true; } cArray(int size, int inc = 1, bool autodelete = true) { Array = NULL; Size = 0; Increment = inc; Last = 0; CurIndex = 0; AutoDelete = autodelete; Increase(size); } cArray(const cArray &cA) { Array = cA.Array; Size = cA.Size; Increment = cA.Increment; Last = cA.Last; CurIndex = cA.CurIndex; AutoDelete = cA.AutoDelete; } ~cArray(void) { if (AutoDelete) SAFE_DELETE_ARRAY(Array); } void Insert(const T &In, const int &Index); void Delete(const int &Index); void InsertSafe(const T &In, const int &Index); void DeleteSafe(const int &Index); __inline void Clear() { if (Size > 0) { SAFE_DELETE_ARRAY(Array); Size = 0; } } __inline T& operator[](const int &Index) { return Array[Index]; } __inline T *Get(const int &Index) { return &Array[Index]; } __inline T *GetFirst() { return &Array[0]; } __inline T *GetLast() { return &Array[Last]; } __inline bool GetSafe(T &Out, const int &Index) { if (Index >= 0 && Index < Size) { Out = Array[Index]; return true; } else return false; } __inline bool GetSafeFirst(T &Out) { if (Size > 0) { Out = Array[0]; return true; } else return false; } __inline bool GetSafeLast(T &Out) { if (Size > 0) { Out = Array[Last]; return true; } else return false; } __inline const int &GetSize() { return Size; } __inline const int &GetIncrement() { return Increment; } __inline const int &GetFilledSize() { return CurIndex; } __inline void operator++() { Ghost->AdjustPointer(Array, Size, 1); Last = Size - 1; } __inline void operator--() { Ghost->AdjustPointer(Array, Size, -1); Last = Size - 1; } __inline void Increase(int inc = 0) { if (inc == 0) inc = Increment; Ghost->AdjustPointer(Array, Size, inc); Last = Size - 1;} __inline void Decrease(int inc = 0) { if (inc == 0) inc = -Increment; Ghost->AdjustPointer(Array, Size, inc); Last = Size - 1;} __inline void Push(const T &In) { Ghost->AdjustPointer(Array, Size, 1); Last = Size - 1; Array[Last] = In; } __inline void Fill(const T &In) { Array[CurIndex] = In; ++CurIndex; if (CurIndex >= Size) Increase(); } __inline bool IsValidIndex(const int &Index) { return (Index >= 0 && Index < Size); } __inline void SetAutoDelete(const bool &In) { AutoDelete = In; } __inline void SetIncrement(const int &i) { if (i != 0) Increment = i; } __inline void SetCurIndex(const int &i = 0) { if (IsValidIndex(i)) CurIndex = i; }private: T *Array; int Size; int Increment; int Last; int CurIndex; bool AutoDelete;};template<class T> void cArray<T> :: Insert(const T &In, const int &Index){ T *NewArray = new T[Size + 1]; memcpy(&NewArray[0], &Array[0], sizeof(T) * Index); NewArray[Index] = In; memcpy(&NewArray[Index + 1], &Array[Index], sizeof(T) * Size - Index); SAFE_DELETE_ARRAY(Array); Array = NewArray; ++Size;}template<class T> void cArray<T> :: Delete(const int &Index){ if (Size == 1) { SAFE_DELETE_ARRAY(Array); Size = 0; return; } --Size; T *NewArray = new T[Size]; if (Index != 0 && Index < Size) { memcpy(&NewArray[0], &Array[0], sizeof(T) * Index); memcpy(&NewArray[Index], &Array[Index + 1], sizeof(T) * (Size - Index)); } else if (Index == 0) memcpy(&NewArray[0], &Array[1], sizeof(T) * (Size)); else if (Index == Size) memcpy(&NewArray[0], &Array[0], sizeof(T) * (Size)); SAFE_DELETE_ARRAY(Array); Array = NewArray;}template<class T> void cArray<T> :: InsertSafe(const T &In, const int &Index){ if (Index >= 0 && Index < Size) { T *NewArray = new T[Size + 1]; memcpy(&NewArray[0], &Array[0], sizeof(T) * Index); NewArray[Index] = In; memcpy(&NewArray[Index + 1], &Array[Index], sizeof(T) * Size - Index); SAFE_DELETE_ARRAY(Array); Array = NewArray; ++Size; }}template<class T> void cArray<T> :: DeleteSafe(const int &Index){ if (Index >= 0 && Index < Size) { if (Size == 1) { SAFE_DELETE_ARRAY(Array); Size = 0; return; } --Size; T *NewArray = new T[Size]; if (Index != 0 && Index < Size) { memcpy(&NewArray[0], &Array[0], sizeof(T) * Index); memcpy(&NewArray[Index], &Array[Index + 1], sizeof(T) * (Size - Index)); } else if (Index == 0) memcpy(&NewArray[0], &Array[1], sizeof(T) * (Size)); else if (Index == Size) memcpy(&NewArray[0], &Array[0], sizeof(T) * (Size)); SAFE_DELETE_ARRAY(Array); Array = NewArray; }}
Any help would be great!
Thanks,
Jake