I implemented singly linked list using binary-tree concept to insert elements in the list (tree). When I copy the values of a list using assignment like that
Tree<A> list1, list2;
//Filling list1...
list2 = list1;
//Remove all elements of list1 using the implemented remove method...
print(list2); //throws an error as its elements appear to have been removed as well
It seems to also copy the pointers of class members (m_pLeft and m_pRight) of the tree. Does anyone know why ? and how can I avoid it?
template<typename T>
class Tree {
class ListElem {
public:
ListElem(T t) : m_Elem(t) {}
ListElem(const ListElem&) = delete;
ListElem& operator=(const ListElem&) = delete;
T m_Elem;
ListElem* m_pLeft = nullptr, * m_pRight = nullptr;
};
public:
unsigned size() const {
return this->length;
}
void remove(T t)
{
if (this->length > 0)
{
this->m_pHead = remove(this->m_pHead, t);
}
}
private:
ListElem* m_pHead = nullptr;
unsigned length = 0;
};
The full code can be found here.