Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/this"

From cppreference.com
< cpp‎ | language
(Created page with "Inside non-static class methods, holds a pointer to the class object from which the method was invoked.<br/> It's type is a constant pointer to {{cpp|T}} (where T is the class na...")
 
(Added CWG issue #2869 DR.)
 
(43 intermediate revisions by 15 users not shown)
Line 1: Line 1:
Inside non-static class methods, holds a pointer to the class object from which the method was invoked.<br/>
+
{{title|The {{tt|this}} pointer}}
It's type is a constant pointer to {{cpp|T}} (where T is the class name) with the same ''cv-specifier'' as the in method signature.
+
{{cpp/language/classes/navbar}}
  
It can be used to explicitly access class members, but it can be omitted
+
===Syntax===
 +
{{sdsc begin}}
 +
{{sdsc|{{ttb|this}}}}
 +
{{sdsc end}}
  
==Types for {{cpp|this}}==
+
The expression {{c|this}} is a {{rlps|value category#prvalue}} {{rlp|expressions|expression}} whose value is the address of the {{rlpsd|overload resolution#Implicit object parameter}} (object on which the implicit object member function is being called). It can appear in the following contexts:
* {{cpp|void T::foo()}}
+
@1@ Within the body of any {{rlp|member functions|implicit object member function}}, including {{rlp|initializer list|member initializer list}}{{rev inl|since=c++11|, and {{rlp|lambda#closure type fun operator|lambda expression body}}}}.
:: {{cpp|T* const this;}}
+
@2@ Within the {{rlp|function|declaration}} of any implicit object member function anywhere after the (optional) cv-qualifier sequence, including the {{rlp|exceptions|exception specification}}{{rev inl|since=c++11| and the trailing return type}}.
* {{cpp|void T::foo() const}}
+
:: {{cpp|const T* const this;}}
+
* {{cpp|void T::foo() volatile}}
+
:: {{cpp|volatile T* const this;}}
+
* {{cpp|void T::foo() const volatile}}
+
:: {{cpp|const volatile T* const this;}}
+
  
==Example==
+
{{rrev|since=c++11|
{{source cpp|code=
+
@3@ Within {{rlp|data members#Member initialization|default member initializer}}.
class T
+
@4@ Within {{rlp|lambda#Lambda capture|capture list}} of a lambda expression.
{
+
}}
    int x;
+
  
     void foo()
+
===Explanation===
 +
{{c|this}} can only associate with the innermost enclosing class of its appearance, even if the appearance is invalid in the context:
 +
{{source|1=
 +
class Outer
 +
{
 +
    int a[sizeof(*this)];            // Error: not inside a member function
 +
    unsigned int sz = sizeof(*this); // OK: in default member initializer
 +
   
 +
     void f()
 
     {
 
     {
         this->x = 5; // this used explicitly
+
         int b[sizeof(*this)];     // OK
         x = 6; // same as this->x = 6;
+
          
 +
        struct Inner
 +
        {
 +
            int c[sizeof(*this)]; // Error: not inside a member function of Inner
 +
                                  // “this” is not associated with Outer
 +
                                  // even if it is inside a member function of Outer
 +
        };
 
     }
 
     }
 +
};
 +
}}
  
    void foo() const
+
The type of {{c|this}} in a member function of class {{tt|X}} is {{tt|X*}} (pointer to X). If the member function is {{rlp|member functions#member functions with cv-qualifiers|declared with a cv-qualifier sequence}} ''cv'', the type of {{c|this}} is {{tt|''cv'' X*}} (pointer to identically cv-qualified X). Since constructors and destructors cannot be declared with cv-qualifiers, the type of {{c|this}} in them is always {{tt|X*}}, even when constructing or destroying a const object.
 +
 
 +
In class templates, {{c|this}} is a {{rlp|dependent name|dependent expression}}, and explicit {{c|this->}} may be used to force another expression to become dependent.
 +
{{source|1=
 +
template<typename T>
 +
struct B
 +
{
 +
    int var;
 +
};
 +
 
 +
template<typename T>
 +
struct D : B<T>
 +
{
 +
    D()
 
     {
 
     {
         this->x = 7; // Error: *this is constant
+
         // var = 1;   // Error: “var” was not declared in this scope
 +
        this->var = 1; // OK
 
     }
 
     }
 +
};
 +
}}
  
     void foo ( int x )
+
{{rlp|constructor|During construction}} 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 {{c|this}} pointer, the value of the object or subobject thus obtained is unspecified. In other words, the this pointer cannot be aliased in a constructor:
 +
{{source|1=
 +
extern struct D d;
 +
 
 +
struct D
 +
{
 +
    D(int a) : a(a), b(d.a) {} // b(a) or b(this->a) would be correct
 +
    int a, b;
 +
};
 +
 
 +
D d = D(1); // because b(d.a) did not obtain a through this, d.b is now unspecified
 +
}}
 +
 
 +
It is possible to execute {{c|delete this;}}, if the program can guarantee that the object was allocated by {{c/core|new}}, however, this renders every pointer to the deallocated object invalid, including the {{c|this}} pointer itself: after {{c|delete this;}} returns, such member function cannot refer to a member of a class (since this involves an implicit dereference of {{tt|this}}) and no other member function may be called.
 +
 
 +
This can be used in the member function of the reference-counting pointer {{rev inl|since=c++11|(for example, {{lc|std::shared_ptr}})}} responsible for decrementing the reference count, when the last reference to the managed object goes out of scope.
 +
 
 +
{{source|1=
 +
<!-- libreoffice i18nlangtag/source/languagetag/simple-langtag.cxx -->
 +
class ref
 +
{
 +
    // ...
 +
     void incRef() { ++mnRef; }
 +
    void decRef() { if (--mnRef == 0) delete this; }
 +
};
 +
}}
 +
 
 +
===Keywords===
 +
{{ltt|cpp/keyword/this}}
 +
 
 +
===Example===
 +
{{source
 +
|
 +
|code=
 +
class T
 +
{
 +
    int x;
 +
   
 +
    void foo()
 
     {
 
     {
         // parameter x shadows the attribute with the same name
+
         x = 6;      // same as this->x = 6;
         this->x = x; // unqualified x refers to the parameter, but the attribute is still accessible using the this pointer
+
         this->x = 5; // explicit use of this->
 
     }
 
     }
 
+
   
     T& operator= ( const T& b )
+
     void foo() const
 
     {
 
     {
        x = b.x;
+
    //  x = 7; // Error: *this is constant
        return *this; // this is often used when a reference to the current object is needed
+
 
     }
 
     }
 
+
   
     void bar ( const T& b )
+
     void foo(int x) // parameter x shadows the member with the same name
 
     {
 
     {
         // two ways to call class operators using this
+
         this->x = x; // unqualified x refers to the parameter
        (*this) = b;
+
                    // “this->” is required for disambiguation
        this->operator= ( b );
+
 
     }
 
     }
 
+
   
     void bar()
+
    int y;
 +
    T(int x) : x(x),      // uses parameter x to initialize member x
 +
              y(this->x) // uses member x to initialize member y
 +
    {}
 +
   
 +
     T& operator=(const T& b)
 
     {
 
     {
      // methods can be called even when there's no object
+
        x = b.x;
      // the this pointer can be seen like an additional parameter implicitly passed
+
        return *this; // many overloaded operators return *this
      if ( this == NULL )
+
          return;
+
 
     }
 
     }
 
};
 
};
 +
}}
  
T* pointer = reinterpret_cast<T*>(123);
+
===Defect reports===
pointer->bar(); // bar is called with 123 (0x7B) as value for this
+
{{dr list begin}}
 +
{{dr list item|wg=cwg|dr=760|std=C++98|before=when {{c|this}} is used in a nested class, it was<br>unspecified whether it is associated with<br>the nested class or the enclosing class|after={{c|this}} always associates with<br>the innermost nested class,<br>regardless of whether it is in<br>a non-static member function}}
 +
{{dr list item|wg=cwg|dr=2271|std=C++98|before={{c|this}} could be aliased when<br>constructing a non-const object|after=alias is also<br>prohibited in this case}}
 +
{{dr list item|wg=cwg|dr=2869|std=C++98|before=it was unclear whether {{c|this}} could be used in a<br>static member function of a non-associated class|after=made clear}}
 +
{{dr list end}}
  
}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 01:57, 12 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Contents

[edit] Syntax

this

The expression this is a prvalue expression whose value is the address of the implicit object parameter (object on which the implicit object member function is being called). It can appear in the following contexts:

1) Within the body of any implicit object member function, including member initializer list, and lambda expression body(since C++11).
2) Within the declaration of any implicit object member function anywhere after the (optional) cv-qualifier sequence, including the exception specification and the trailing return type(since C++11).
4) Within capture list of a lambda expression.
(since C++11)

[edit] Explanation

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

class Outer
{
    int a[sizeof(*this)];            // Error: not inside a member function
    unsigned int sz = sizeof(*this); // OK: in default member initializer
 
    void f()
    {
        int b[sizeof(*this)];     // OK
 
        struct Inner
        {
            int c[sizeof(*this)]; // Error: not inside a member function of Inner
                                  // “this” is not associated with Outer
                                  // even if it is inside a member function of Outer
        };
    }
};

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

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

template<typename T>
struct B
{
    int var;
};
 
template<typename T>
struct D : B<T>
{
    D()
    {
        // var = 1;    // Error: “var” was not declared in this scope
        this->var = 1; // OK
    }
};

During construction 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 this pointer, the value of the object or subobject thus obtained is unspecified. In other words, the this pointer cannot be aliased in a constructor:

extern struct D d;
 
struct D
{
    D(int a) : a(a), b(d.a) {} // b(a) or b(this->a) would be correct
    int a, b;
};
 
D d = D(1); // because b(d.a) did not obtain a through this, d.b is now unspecified

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

This can be used in the member function of the reference-counting pointer (for example, std::shared_ptr)(since C++11) responsible for decrementing the reference count, when the last reference to the managed object goes out of scope.

class ref
{
    // ...
    void incRef() { ++mnRef; }
    void decRef() { if (--mnRef == 0) delete this; }
};

[edit] Keywords

this

[edit] Example

class T
{
    int x;
 
    void foo()
    {
        x = 6;       // same as this->x = 6;
        this->x = 5; // explicit use of this->
    }
 
    void foo() const
    {
    //  x = 7; // Error: *this is constant
    }
 
    void foo(int x) // parameter x shadows the member with the same name
    {
        this->x = x; // unqualified x refers to the parameter
                     // “this->” is required for disambiguation
    }
 
    int y;
    T(int x) : x(x),      // uses parameter x to initialize member x
               y(this->x) // uses member x to initialize member y
    {}
 
    T& operator=(const T& b)
    {
        x = b.x;
        return *this; // many overloaded operators return *this
    }
};

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 760 C++98 when this is used in a nested class, it was
unspecified whether it is associated with
the nested class or the enclosing class
this always associates with
the innermost nested class,
regardless of whether it is in
a non-static member function
CWG 2271 C++98 this could be aliased when
constructing a non-const object
alias is also
prohibited in this case
CWG 2869 C++98 it was unclear whether this could be used in a
static member function of a non-associated class
made clear