[英]How to assign pointer to this in C++?
我正在尝试实现treap类,所以我有以下代码:
void item::insert(item* it) {
if (!this)
this = it;
.....
我无法使用GCC进行编译:
error: lvalue required as left operand of assignment
我知道,我可以简单地分配给所有成员it
到this
,但有另一种方式?
我知道,我可以简单地分配给所有成员
it
到this
,但有另一种方式?
您正在寻找*this = *it
。 this
不是左值。
您可能应该使用item const&
而不是item*
:
void item::insert(item const& it) {
*this = it;
}
作为一个方面说明: this
绝不可能在任何明确的程序空指针,因为方法只能呼吁的对象。 空指针不指向对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.