+ 3
What is the usages and advantages of chained assignment in below code?clinkedlist is class name and pushback adds node
What is the usages and advantages of chained assignment in below code?and what is difference between when we return *this and when we return x object in chained assignment?this function is in class: Clinkedlist operator=(Clinkedlist x) { this->~Clinkedlist(); Cnode* t=x.head; while(t) { this->pushback(t->getdata(); t=t->getnext(); } return *this; }
1 Answer
0
This is a normal operator overloading. However, if your linked list is standard, then there is no need for the while loop at all: acquiring the head of the link is enough.
As for returning (*this) : it is not the best thing you can do. This will return a copy of the current insurance that then will be reassigned to essentially the same copy; so I recommend returning (clinkedlist &) instead.