【问题标题】:Can not assign to this: lvalue required as left operand of assignment不能赋值给这个:左值需要作为赋值的左操作数
【发布时间】:2013-12-03 17:05:39
【问题描述】:
class scope{  
    //...
    scope* parent;  
    //...
public:  
    //...
    void leavescope();  
    //...
};  
void scope::leavescope(){  
    this = this->parent; // complains about this line  
}

我正在尝试实现作用域,但在离开作用域并返回外层作用域时遇到了麻烦。

【问题讨论】:

  • 编译器是对的,不能赋值this。要解决此问题,您需要更好地描述您的用例。您是否正在创建许多 scope 对象?好像是这样的。通常你只是让一个超出范围,这会破坏它。

标签: c++ class scope variable-assignment lvalue


【解决方案1】:

thisprvalue 或纯右值,不能修改,draft C++ standard 部分 9.3.2 this 指针 说:

在非静态 (9.3) 成员函数的主体中,关键字 this 是一个纯右值表达式,其值是调用该函数的对象的地址。 [...]

完全不清楚您为什么要修改 this

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多