Learner
Learner

Reputation: 59

Significance of = operator overloading in case of linked lists

I was trying to overload = operator in linked list in C++ and wrote the below code.

template<class T>
class List {

    public:
        List();
        List (T t_data);
 List& operator=(const List<T> &L);
 private:
        template<class L>
            class Node {

                public:
                    L data;
                    Node *next;
                    Node *prev;
                    Node(T t_data) {
                        data = t_data;
                        next = prev = NULL;
                    }
            };

        Node<T> *head;


};

template<class T>
List&  List<T>::operator=(const List<T> &L) {
    Node<T> *t_head = head;
    Node<T> *t_tail = head->prev;
    Node<T> *temp;
    while(t_head ! = t_tail) {
        temp = t_head;
        t_head = t_next;
        delete temp;

    }
    head = L.head;
    t_head = t_tail = temp = NULL;

    return *this;
}

I wrote this code just for practicing templates, pointers and operator overloading, but I want to know the significance of the = operator in this case. As even if I use it like

List L1(2);
List L2(3);
L1 = L2;

Now any changes reflected in L1 will be reflected in L2, so instead of that we can do

List L1(2);
List *L2 = &L1;

This will also solve the above purpose. So why is the = operator of linked lists overloaded in many articles and books?

Edit: @T.C. In reference to your note, if I will not declare Node as a template , the code will be like this

class List {
public:
    List();
    List (T t_data);
    List& operator=(const List<T> &L);
private:
    class Node {
    public:
        T data;
        Node *next;
        Node *prev;
        Node(T t_data) {
            data = t_data;
            next = prev = NULL;
        }
    };
    Node *head;
};

Now if I declare an object of Node in a member function like below

void List::func() {
    Node temp;
    …..
}

Then how can it be resolved that what is the type of data member “data” of this “temp” object is. Please let me know.

Upvotes: 1

Views: 211

Answers (1)

T.C.
T.C.

Reputation: 137315

In C++, the usual practice is to have value semantics, i.e., after L1 = L2;, changes to L1 would not be visible in L2, and vice versa.

Thus your implementation of operator = is wrong. What it should do is:

  1. Make a copy of the nodes stored in the other linked list.
  2. Destroy all nodes stored in the current linked list
  3. Store the copy made in step 1 into the current linked list

A common idiom is the copy and swap idiom: You create a temporary copy of the other linked list (point 1), swap the contents of the current linked list with the temporary copy (point 3), and when your assignment function returns, the temporary copy is destroyed along with the former contents of the linked list assigned to (point 2).

Can you do it differently? Sure, but it (1) is very unidiomatic and (2) requires far more than what you have now to do correctly. For example, your current implementation of operator = will cause double deletion when L1 and L2's destructors run after an assignment. You'd need some sort of reference counting and additional logic in the destructor to ensure that you don't delete a Node that's still being used, but also do not leak memory.

Side note: Node should not be a template since it should never store something other than a T.

Upvotes: 5

Related Questions