[英]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.