0

I have

template <class T>
class arrList: public linearList<T> {
public: 
      arrList() {}
      arrList(const arrList<T>& List);
      ~arrList() {delete[] element; }
protected:
      void indexCheck(int indx) const;
      T* element;
      int arrLength;
      int listSize;
};

And the copy constructor is

template<class T>
inline arrList<T>::arrList(const arrList<T>& List) {
       element = List.element;
       arrLength = List.arrLength;
       listSize = List.listSize;
}

But im not sure if this is correct for T* element, and also if I have to insert the void function in the copy constructor too. I am new to this, and I don't know a lot about it, so any kind of help will be appreciated.

  • What you have implemented is identical to `arrList(const arrList& List) = default;`. – 273K May 13 '22 at 20:52
  • 1
    this should help you: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – NathanOliver May 13 '22 at 20:52
  • Does this answer your question? [What is the difference between a deep copy and a shallow copy?](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) – BoP May 13 '22 at 20:53
  • The copy constructor you designed creates multiple objects that will each try to `delete` the same `element` pointer upon destruction. This will not go well. – Drew Dormann May 13 '22 at 21:08
  • Where is the copy for the `linearList` part of the class? And you should probably have a move constructor instead of copy constructor. For a copy constructor like you have you need `std::shared_ptr element;`. – Goswin von Brederlow May 13 '22 at 21:53

0 Answers0