This question came in my mind due to the following error in my c++ program
#include <iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj(15);
Test *ptr = new Test (100);
obj.change(ptr);
obj.print();
return 0;
}
Error:
main.cpp:18:31: error: lvalue required as left operand of assignment
18 | void change(Test *t) { this = t; }
|
I searched about this error and found that it generally occurs when we try to assign the constants on the left hand side of the assignment operators to the variable on the right hand side.



According to 9.3.2 The this pointer [class.this]
So as the error says, the left hand side should be an lvalue but you're giving it a prvalue because
changeis a non-static member function and inside any non-static member function the keywordthisis a prvalue according to the above quoted statement. Hence the error.But you can modify the object that
thispoints to, as shown below:Note in the above example i have replaced
this = t;withThis time the program works because now the left hand side is an lvalue.
Some More Explanation
From IBM's this pointer documentation,
The
thisparameter has the typeTest *const. That is, it is a constant pointer to aTestobject. Now since it is constant pointer, you cannot assign a different pointer value to it usingthis = t;. And hence the error.Similarly, from Microsoft's this pointer documentation,
So this also explains the error you're getting.