1) How this
pointer is different from other pointers? As I understand pointers point to the memory in heap. Does that mean objects are always constructed in heap, given that there is pointer to them?
2)Can we steal this
pointer in move constructor or move assignment?
How this pointer is different from other pointers?
The this
pointer only exists in the context of a non-static
class member function. It is also implicit, it's name is a reserved keyword and it is always a prvalue expression. Otherwise, it's the same as any other pointer.
As I understand pointers point to the memory in heap.
Pointers can point to anything in memory. It's not limited to the heap and neither are objects.
Can we steal this pointer in move constructor or move assignment?
this
is always a prvalue expression. It's not possible to assign a new address to it any more than you could assign a new value to 5
. The fact is objects exist in one place in memory for their whole life time. Their address can never change and it would be illogical to try to change that by assigning a new address to this
. Moving from an object moves the value or state that object has elsewhere, but the object itself still exists at it's previous address.