C++ Reference ยท Keywords and Statements
The this pointer
Reference for the C++ this pointer: the hidden parameter of member functions, method chaining, const and ref-qualified members, explicit object parameters and *this in lambdas.
Inside a non-static member function, this is a pointer to the object the function was called on. Every call like obj.f() passes the address of obj as a hidden first argument; this is that argument. Member names such as value are shorthand for this->value.
You need this explicitly to return the current object (for method chaining), to pass it to other code, to disambiguate a member from a parameter with the same name, and to refer to members of a dependent base class in templates.
struct Builder { Builder& add(int n) { total_ += n; return *this; } // return the object itself int total_ = 0; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
this // prvalue of type T* (const T* in const members) *this // the object itself (an lvalue) auto f(this Self&& self) // C++23 explicit object parameter (replaces the hidden one)
Type: T* in an ordinary member, const T* in a const member, volatile T* for volatile members. It is a prvalue: you cannot assign to it or take its address.
Usage notes
thisexists only in non-static member functions, constructors, destructors, default member initializers and lambdas inside them. Static members have nothis.- Returning
*thisby reference gives method chaining (obj.a().b().c()). Return*thisby value only when you want a copy; returnT&for assignment-style operators. - Deleting
this(delete this;) is legal only when the object was allocated withnewand nothing touches it afterward. Avoid it unless you manage lifetime deliberately. - A lambda must capture
thisexplicitly to use members:[this]stores the pointer (dangling if the lambda outlives the object),[*this]copies the object (C++17). C++20 deprecated the implicit capture ofthisthrough[=]. - In class templates with dependent bases, unqualified names are not found in the base; write
this->nameorBase<T>::name. - Do not compare or use
thisbefore the object is constructed or after it is destroyed in surprising ways; calling virtual functions in constructors dispatches to the class being constructed. - C++23 explicit object parameters (
void f(this Foo& self)) make the object an ordinary named parameter, enabling deducedthis, recursive lambdas and one function forconst/non-constoverloads. It compiles with GCC 15 in C++23 mode. - Ref-qualifiers (
void f() &;,void f() &&;) decide whether*thisis an lvalue or rvalue for overload selection.
Related
- static
- const
- Lambdas
- Tutorial: The this pointer, Static members