I was learning about this pointer that it contains the address of the current object that is invoking the function. But I have a doubt regarding this pointer when I am returning the current object from the member function.
#include<bits/stdc++.h>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int x,int y)
{
this->x = x;
this->y = y;
//cout<<this<<endl;
}
Point setX(int x)
{
this->x = x;
//cout<<this<<endl;
return *this;
}
Point setY(int y)
{
this->y = y;
//cout<<this<<endl;
return *this;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p(10,20);
cout<<p.getX()<<" "<<p.getY()<<endl;
p.setX(1000).setY(2000);
cout<<p.getX()<<" "<<p.getY();
return 0;
}
Why p.setX(1000).setY(2000) is only modifying the value of x, not y? Why in the second the cout statement answer is 1000 20 but it should be 1000 2000?