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; }
2 Antworten
+ 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.
0
thnx