W W this
wskaźnik [class.this], C++ standardowe stanowi:Jeśli `this` nie jest const, dlaczego nie mogę go zmodyfikować?
Typ
this
w funkcji składowej klasęX
jest .
tj. this
nie jest const
. Ale dlaczego to jest wtedy
struct M {
M() { this = new M; }
};
daje
error: invalid lvalue in assignment <-- gcc
'=' : left operand must be l-value <-- VC++
'=' : left operand must be l-value <-- clang++
'=' : left operand must be l-value <-- ICC
(source: some online compiler frontends)
Innymi słowy, this
nie const
, ale to naprawdę jest!
słowo kluczowe "this" nie jest zmienną, dlatego nie możemy stwierdzić, że jest stałe lub nie. –
nawet gdyby to działało, skonstruowałbyś tylko nieskończoną pętlę. –
@vlad_tepesch: Albo to, albo nieskończona rekurencja (jak można to zrobić w pythonie). To jednak nie było pytanie. –