/* Note: This particular code sample was adopted from the Learn CPP website. https://www.learncpp.com/cpp-tutorial/133-template-classes/ Note that we named this as a .hpp file. For our purposes we are using this naming convention to mean that the file contains both traditional header information as well as function definitions (that would usually be placed in a corresponding .cpp file). Since we are working with a template class, C++ has certain requirements, one of which is that the template code needs to be contained all in one file. There are certain work-arounds but this requirement is something to be aware of. */ #ifndef CUSTOM_ARRAY_H #define CUSTOM_ARRAY_H #include using namespace std; template class Custom_Array { private: int m_length; T *m_data; public: //default constructor Custom_Array() { this->m_length = 0; this->m_data = nullptr; } //non-default constructor Custom_Array(int length) { this->m_data = new T[length]; this->m_length = length; } ~Custom_Array() { cout << "Destructor" << endl; delete[] this->m_data; this->m_data = nullptr; } void Erase() { delete[] m_data; // We need to make sure we set m_data to 0 here, otherwise it will // be left pointing at deallocated memory! m_data = NULL; m_length = 0; } T& operator[](int index) { return this->m_data[index]; } // The length of the array is always an integer // It does not depend on the data type of the array int getLength(); }; template //template prefix int Custom_Array::getLength() { return this->m_length; } #endif