Say the object is
class A {
public : void Silly(){
this = 0x12341234;
}
I know I will get compiler error ‘ “this” is not a lvalue.’ But then it is not a temporary either. So what is the hypothetical declaration of “this” ?
Compiler : GCC 4.2 compiler on mac.
For some class X,
thishas the typeX* this;, but you’re not allowed to assign to it, so even though it doesn’t actually have the typeX *const this, it acts almost like it was as far as preventing assignment goes. Officially, it’s aprvalue, which is the same category as something like an integer literal, so trying to assign to it is roughly equivalent to trying to assign a different value to'a'or10.Note that in early C++,
thiswas an lvalue — assigning tothiswas allowed — you did that to handle the memory allocation for an object, vaguely similar to overloadingnewanddeletefor the class (which wasn’t supported yet at that time).