Advertisement
MaGuSware2012

2D Array Class

Feb 9th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /**
  2.   * This class was created for anybody to use at any time, there is no licensing to it. Just use it as you see fit.
  3.   * Created Chris Armitt.
  4.   */
  5.  
  6. #ifndef _ARRAY2D
  7. #define _ARRAY2D
  8.  
  9. template<class T>
  10. class Array2D
  11. {
  12. protected:
  13.     T** m_pTheArray;
  14.     int m_iX;
  15.     int m_iY;
  16.  
  17. public:
  18.     Array2D(int x, int y)
  19.     {
  20.         m_pTheArray = new T*[x];
  21.         for (int i = 0; i < x; i++)
  22.         {
  23.             m_pTheArray[i] = new T[y];
  24.             for (int j = 0; j < y; j++)
  25.             {
  26.                 m_pTheArray[i][j] = T(); //default;
  27.             }
  28.         }
  29.  
  30.         m_iX = x;
  31.         m_iY = y;
  32.     }
  33.  
  34.     virtual ~Array2D()
  35.     {
  36.         for (int i = 0; i < m_iX; i++)
  37.         {
  38.             delete[] m_pTheArray[i];
  39.         }
  40.  
  41.         delete[] m_pTheArray;
  42.     }
  43.  
  44.     bool SetElement(int x, int y, T item)  
  45.     {
  46.         if (x >= m_iX || y >= m_iY || y < 0 || x < 0)
  47.         {
  48.             return false;
  49.         }
  50.  
  51.         m_pTheArray[x][y] = item;
  52.         return true;
  53.     }
  54.  
  55.     bool GetElement(int x, int y, T &item)
  56.     {
  57.         if (x >= m_iX || y >= m_iY || y < 0 || x < 0)
  58.         {
  59.             return false;
  60.         }
  61.  
  62.         item = m_pTheArray[x][y];
  63.         return true;
  64.     }
  65. };
  66.  
  67. #endif //#ifndef _ARRAY2D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement