Keyword this

Hi, as a beginner of C++ I found the C++ Language Tutorial on this site is extremely helpful. As I progress through the tutorial, I seemed to get stuck at the Classes (II) section with these lines of code:

CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}

Would someone kind enough to explain?

Thank you.
this points to the current class instance.
I understand this points to the current class instance. But I am missing a link of how that x=param.x;
y=param.y; contributed it.
Not sure what you're asking. x and y are assigned new values. The function returns a reference to its class instance.
That's all.
closed account (D80DSL3A)
x and y are the data members of a CVector class object. When data members appear directly in a member function they are the members of the object calling the function (this).

In your example x and y are the same as this->x and this->y
The function could be written like this as well:
1
2
3
4
5
6
CVector& CVector::operator= (const CVector& param)
{
    this->x=param.x;// explicit use of this pointer
    this->y=param.y;
    return *this;// the data members were just assigned above
}

Hope that made sense.
I went over the section again and figured it out with help from your comments. I got sidetracked by analyzing too much the content of the function. As fun2code stated: x and y are the data members of a CVector class object. That piece of code was to demonstrate the use of "this"...

Thanks.
Topic archived. No new replies allowed.