What is the usages and advantages of chained assignment in below code?clinkedlist is class name and pushback adds node | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

11th May 2020, 7:10 AM
PGS ADI
PGS ADI - avatar
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.
16th May 2020, 6:06 AM
Cihad Jasem Alhaji
Cihad Jasem Alhaji - avatar