I was learning about this pointer in C++ that it contains address of class object. But I have a problem understanding my code.
#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;
}
Point setX(int x)
{
this->x = x;
return *this;
}
Point setY(int y)
{
this->y = y;
return *this;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p(10,20);
cout<<p.getX()<<" "<<p.getY()<<endl;
cout<< p.setX(1000).getX();
return 0;
}
I am returning pointer object from setX. And p.setX will first modify the value of x and then it will return a copy of object(p) from it. Since this is a copy then why my answer is 1000 in p.setX(1000).getx(). Since setX will modify the value of x in original object p.