In a function, how to you assign this
a new value?

- 90,923
- 26
- 142
- 180

- 303
- 1
- 3
- 4
-
16The proper question, I suppose, would be 'why would you ever want to do that'? – raina77ow Nov 20 '12 at 15:51
-
1This might be insightful, but the answer I was going to comment on was deleted: If it were `const`, then you could simply `const_cast` it back to something modifiable. The result would be undefined behavior, but it wouldn't be a syntax error. Applying `const_cast` to `this` OTOH will not compile in a well-behaved compiler. – Potatoswatter Nov 20 '12 at 15:54
-
6Sounds like the [X-Y Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me! – Rook Nov 20 '12 at 16:07
6 Answers
You can assign the object this
points at:
*this = XY;
But you can't assign the direct value of this
:
this = &XY; // Error: Expression is not assignable

- 14,614
- 10
- 34
- 46
-
14+1 for `*this = XY`, which gives the actual answer for one interpretation of the question (didn't seem to be the OP's interpretation, but was my interpretation). – M Katz Jun 14 '16 at 18:37
You can't.
9.3.2 The this pointer [class.this]
1 In the body of a non-static (9.3) member function, the keyword
this
is a prvalue expression whose value is the address of the object for which the function is called. [...] (emphasis & link mine)
You can modify the object this
points to, which is *this
. For example:
struct X
{
int x;
void foo()
{
this->x =3;
}
};
The method modifies the object itself, but something like this = new X
is illegal.

- 1
- 1

- 253,575
- 64
- 457
- 625
-
+1: There is a nearly identical passage in the C++03 Standard. Except of "prvalue" it says "non-lvalue" – John Dibling Nov 20 '12 at 16:13
Long ago, before the first C++ standard has been published, some compiler implementations allowed you to write the following code inside a constructor:
this = malloc(sizeof(MyClass)); // <<== No longer allowed
The technique served as the only way to control allocation of class of objects. This practice has been prohibited by the standard, because overloading of the operator new
has solved the problem that used to be tackled by assignments to this
.

- 714,442
- 84
- 1,110
- 1,523
You can't. If you feel the need to do this perhaps you should be writing a static method taking a class pointer as it's first parameter.

- 7,897
- 29
- 27
-
However, it should be a reference (or a pointer) to a class pointer in order to be modifiable. – leemes Nov 20 '12 at 15:56
-
It would be modifyable locally, even as a straight pointer. I was thinking of the situation where you have some recursive data structure, and you want to call a recursive or iterative routine, The initial pointer you pass to the routine is `this` but afterwards it recurses or iterates with different values for the pointer. But it seems I was completely wide of the mark. The OP has a more basic misunderstanding. – john Nov 20 '12 at 15:59
You cannot assign value to this
pointer. If you try to assign the value to the this
somthing like this = &a
it would result in illegal expression

- 168,305
- 31
- 280
- 331
-
I am writing a function that supposedly must modify this. Are you saying that is not possible? – Astra Meyers Nov 20 '12 at 15:51
-
3@AstraMeyers Completely impossible. Probably you misunderstood and are supposed to modify what `this` points to. – john Nov 20 '12 at 15:52
-
@AstraMeyers Why should your function modify `this` value - and not the properties of the object which it refers to? – raina77ow Nov 20 '12 at 15:53
-
1You probably want to mody the state of 'this' instead of trying to modify where 'this' points to – dchhetri Nov 20 '12 at 15:54
You can not. "this" is a hidden argument to every member function of a class and its type for an object of Class X is X* const. This clearly means that you can not assign a new vale to "this" as it is defined as a const. You can however modify the value pointed to by this. Refer http://www.geeksforgeeks.org/this-pointer-in-c/ for more details.

- 693
- 2
- 7
- 20