Chiranth Bs
Chiranth Bs

Reputation: 53

Please help me understand operator overloading = in linked list c++

I have a structure like this -

struct IntNode
{
    int data;                   
    IntNode * next;         
};

and in the class -

    class NodeSLList {
    private:``
        IntNode *head, *tail;
//some other functions
    }

I have an object list1 which contains the linked list with few nodes. I have to create another linked list list2 from list1. How do I achieve this using the function-

NodeSLList & operator=(NodeSLList &list){}

I am not able to undestand how to access list2 inside operator = function as I will be passing list1 inside like-

list2=list1; 

How do I access list2??

Upvotes: 2

Views: 130

Answers (4)

Mankka
Mankka

Reputation: 563

The function (operator) NodeSLList & operator=(NodeSLList &list) is called a copy assignment operator. It does not create new objects but assigns to them. So for your first question

I have to create another linked list list2 from list1. How do I achieve this using the function...

The answer is; you don't. First you create a new object list2 and then you can assign to it. The following code will first call the default constructor and then the copy assignment operator:

NodeSLList list2;
list2 = list1;

So actually list2 = list1; is a shorthand for

list2.operator=(list1);

Perhaps this makes it more clear that the function operator= is used on an existing object, and as all other answers has pointed out, you access this object by *this.

As an aside, if you wrote NodeSLList list2 = list1;, you would be calling the copy constructor, which is an other story...

Upvotes: 0

Nitinkumar Ambekar
Nitinkumar Ambekar

Reputation: 1009

Keep in mind that this pointer points to exiting class you are operating on, thus, this->head or simply head is the member of old list which is you want to change, and newList.head is the member of new list that you have to assign.

NodeSLList & operator=(NodeSLList &newList)
{
        //Check if there is something to free?
        if(head != NULL) //this->head != NULL
        {                    
                //free head
        } 
        //Assign new one
        head = newList.head; //this->head = newList.head;
        //Other stuff if any
        return this;
}

Upvotes: 1

cdonat
cdonat

Reputation: 2822

The answer is *this.

Anyway. please don't implement your own linked list. There already is std::list, or if you need a doubly linked list std::deque. You'd need a lot of effort to make your code as high quality as the standard library, let alone define an interface, that is equally well thought out.

So if this is four your school, write your own linked list. We all did that. It is a good way to practice programming with pointers. But please resist the temptation to do something like that later in production code. I have seen it too often, and it is slow and inefficient most of the time, and often pretty buggy as well.

Upvotes: 1

Aiman Al-Eryani
Aiman Al-Eryani

Reputation: 709

this would be a pointer to the left operand, which is list2 in your case. To get list2 itself, simply dereference it using the dereferencing operator *. ie *this

Upvotes: 1

Related Questions