cpp/language/this

Syntax
The expression is a   whose value is the address of the  (object on which the  is being called). It can appear in the following contexts: @1@ Within the body of any, including. @2@ Within the of a  anywhere after the (optional) cv-qualifier sequence, including

Explanation
can only associate with the innermost enclosing class of its appearance, even if the appearance is invalid in the context:

The type of in a member function of class  is  (pointer to X). If the member function is cv, the type of  is  (pointer to identically cv-qualified X). Since constructors and destructors cannot be declared with cv-qualifiers, the type of in them is always, even when constructing or destroying a const object.

When a non-static class member is used in any of the contexts where the keyword is allowed (non-static member function bodies, member initializer lists, default member initializers), the implicit  is automatically added before the name, resulting in a member access expression (which, if the member is a virtual member function, results in a virtual function call).

In class templates, is a, and explicit  may be used to force another expression to become dependent.

of an object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor's pointer, the value of the object or subobject thus obtained is unspecified. In other words, the this pointer cannot be aliased in a constructor:

It is possible to execute, if the program can guarantee that the object was allocated by , however, this renders every pointer to the deallocated object invalid, including the pointer itself: after  returns, such member function cannot refer to a member of a class (since this involves an implicit dereference of ) and no other member function may be called.

This can be used in the member function of the reference-counting pointer responsible for decrementing the reference count, when the last reference to the managed object goes out of scope.