when I try to assign a pointer to this
pointer, My IDE(clion) said Expression is not assignable
. So, Is this
pointer assignable in c++ ? Is it a rvalue?
Asked
Active
Viewed 96 times
2

HolyBlackCat
- 78,603
- 9
- 131
- 207

Flash
- 21
- 3
-
2Why are you trying to assign to `this`? Note that `*this` is assignable; for example, in copy-and-swap idiom, `swap(*this, other)`, causing `*this` to be assigned to a temporary and a temporary to be a new `*this`. `this` remains the same, which is what is not assignable, and you wouldn't really want it to be anyway. – Kaihaku Sep 19 '21 at 08:51
-
*when I try to assign a pointer to this pointer* Why would you do that? What are you trying to achieve? – Eljay Sep 19 '21 at 13:01
-
Back in the olden days, you could assign a value to `this` inside a constructor. That was a hack to provide a way of allocating objects on the free store. Then `operator new` was invented, and assigning to `this` was banned. – Pete Becker Sep 19 '21 at 14:29
1 Answers
4
Yes, this
is an rvalue. Specifically, it's a prvalue:
The keyword
this
is a prvalue expression whose value is the address of the implicit object parameter (object on which the non-static member function is being called).
(From https://en.cppreference.com/w/cpp/language/this)
Hence, this
is not assignable.

mediocrevegetable1
- 4,086
- 1
- 11
- 33