On Tue, 10 Mar 2015 22:29:48 -0700 (PDT), fl <rxjwg98@gmail.com>
wrote:
Hi,
I read C++ for some time, but I still has problem in understand it.
Below is from web tutorial. It requires to debug it, but I cannot figure out
what is wrong with the void change function.
The error message says that this must be a lvalue. Does that mean 'this'
cannot be assigned a value? Then the change function is invalid?
What it wants me to do?
Please at least give a little hint on it.
thanks,
#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(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
"9.3.2 The this pointer
In the body of a nonstatic (9.3) member function, the keyword this is
a nonlvalue expression whose value is the address of the object for
which the function is called."
this is not an lvalue, IOW, you can't modify it.