Is the value of this
pointer guaranteed to be constant during a lifetime of a particular object? I can't imagine a case where it would change, but don't know whether I am not missing something.
Is the value of
this
pointer guaranteed to be constant during a lifetime of a particular object?
Yes.
As user Aconcagua puts it: the value of this
pointer always is the value of the address of the object on which the function was called on1. So the question is equivalent with:
Can an object change its memory address over life time?
This is not possible, by definition of lifetime
2. The lifetime of an object begins when or after its storage is obtained and ends before of when it is released.
In the body of a non-static (
[class.mfct]
) member function, the keywordthis
is a prvalue whose value is a pointer to the object for which the function is called.
2) [basic.life]/1
(emphasis mine)
The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly multi-dimensional) array thereof, that class type has a trivial default constructor. The lifetime of an object of type
T
begins when:
- storage with the proper alignment and size for type
T
is obtained, and- its initialization (if any) is complete (including vacuous initialization) (
[dcl.init]
), except that if the object is a union member or subobject thereof, its lifetime only begins if that union member is the initialized member in the union ([dcl.init.aggr]
,[class.base.init]
), or as described in[class.union]
.The lifetime of an object
o
of typeT
ends when:
- if
T
is a non-class type, the object is destroyed, or- if
T
is a class type, the destructor call starts, or- the storage which the object occupies is released, or is reused by an object that is not nested within
o
([intro.object]
).
this
every time, regardless of movements in the heap? –
Dinosaurian this
, and dereferencing operations would need to use the "identity" through a lookup table maintaining by the runtime to access the real objects –
Dinosaurian An object has a region of storage. this
points there.
An object occupies a region of storage in its period of construction (
[class.cdtor]
), throughout its lifetime, and in its period of destruction ([class.cdtor]
).
The value of this
is guaranteed to be constant if the program ever reads it, if subsequently some bits of the read value are impossible to garbage collect or if subsequently some bits of the read value escaped outside of the program. In all other cases, it behaves like a Schrödinger's cat, that is, it is constant and variable at the same time.
this
–
Tightlipped © 2022 - 2024 — McMap. All rights reserved.
this
pointer always is the value of the address of the object on which the function was called on. So the question is equivalent with 'can an object change its memory address over life time?' – Milagrostd::move
would changethis
pointers. Formally we would say those are two different objects, but informally one may think of them as "the same," which could breed confusion if one is not paying attention. – Beach