0
为什么我无法修改此指针?出现编译错误“错误:作为赋值左操作数所需的左值”。 以下是节目无法修改此指针
#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;
}
因为你不能; '这'不是一个左值。你希望通过这样做来实现什么? – molbdnilo
但“this”不是一个常量指针? –
您可以更改成员的值('this-> x = t-> x'),但不能指向对象本身。 – anxieux