jweinst1
8/25/2017 - 7:32 PM

A generic container iterable array class in C++

A generic container iterable array class in C++

#include <iostream>

//cpp example class that holds a pnter to an array

template<class T>
class PntArray
{
private:
  T* _array;
  int _size;
public:
  PntArray(int size) : _array(new T[size]), _size(size)
  {}
  ~PntArray()
  {
    delete[] _array;
  }
  //overloads getitem operator for get and set
  T& operator[] (const int index)
  {
    if(index < _size)
    {
      return _array[index];
    }
    else
    {
      throw "Error index too large";
    }
  }
  
  T* getFront()
  {
    return _array;
  }
  
  T* getBack()
  {
    return _array + _size;
  }
};

int main() {
  PntArray<char> arr(7);
  arr[0] = '3';
  arr[1] = 'g';
  std::cout << arr[0] << ", " << arr[1] << "\n";
  //3, g
}