C++ question. I need help writing a function (operator+) that concatenates arrays for a class. More info inside.
template <class T> class CArray { public: // constructors CArray(unsigned size = DEFAULT_SIZE, T element = static_cast<T>(0)); CArray(const CArray<T>& ar); // destructor ~CArray() { if(m_array != nullptr) delete[] m_array; } unsigned getSize() const { return m_size; } void print(ostream& sout, char delimiter = ' ') const; // operators CArray<T> operator=(const CArray<T>& array); T& operator[](unsigned index); const T& operator[](unsigned index) const; CArray<T> operator*=(unsigned scalar); CArray<T> operator*(unsigned scalar) const; CArray<T> operator+(const CArray<T>& array) const; CArray<T> operator+=(const CArray<T>& array); static const int DEFAULT_SIZE = 10; private: // properties T* m_array; unsigned m_size; }; template <class T> ostream & operator<<(ostream & sout, const CArray<T> &arrray); So this is the class. This is what I have for operator+. template <class T> CArray<T> CArray<T>::operator+ (const CArray<T>& array) const { CArray<T> temp(*this); temp.m_size = temp.m_size + array.m_size; for (unsigned i = 0; i < array.m_size; i++) { temp.m_array[m_size+i] = array.m_array[i]; } return temp; } template <class T> CArray<T> CArray<T>::operator+=(const CArray<T>& array) { return *this = *this + array; } }
Join our real-time social learning platform and learn together with your friends!