What is the meaning of the following line in a class member function? Its all about this pointer! But i wanna know the exact ans | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the meaning of the following line in a class member function? Its all about this pointer! But i wanna know the exact ans

{ this->a=b; }

21st Sep 2018, 2:36 PM
Bunny
Bunny - avatar
2 Answers
+ 1
'this' is a special pointer of the class that always points to the class object that calls it inside a method. So in the snippet above, you are trying to access the calling object's member a and then assigns it the value b. Now this is useful in preventing name clashes, ambiguous errors and cases when the compiler is not sure about the attribute being accessed or when you need to call a copy constructor or some other defined method inside another method of the class. Eg : overload of += : { *this = *this + value; } // Assuming the + operator was // already overloaded. Note that 'this' is not usable inside friend functions, as they are not exactly methods of the class.
21st Sep 2018, 4:16 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
thnx
21st Sep 2018, 4:24 PM
Bunny
Bunny - avatar