Skip to content
Tutorials Center
My learning

Topics

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.

C++
struct Builder {
    Builder& add(int n) { total_ += n; return *this; }   // return the object itself
    int total_ = 0;
};

Try it

C++ Chaining and self-reference

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.

C++ this in const members, templates and lambdas

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

C++
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

  • this exists only in non-static member functions, constructors, destructors, default member initializers and lambdas inside them. Static members have no this.
  • Returning *this by reference gives method chaining (obj.a().b().c()). Return *this by value only when you want a copy; return T& for assignment-style operators.
  • Deleting this (delete this;) is legal only when the object was allocated with new and nothing touches it afterward. Avoid it unless you manage lifetime deliberately.
  • A lambda must capture this explicitly 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 of this through [=].
  • In class templates with dependent bases, unqualified names are not found in the base; write this->name or Base<T>::name.
  • Do not compare or use this before 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 deduced this, recursive lambdas and one function for const/non-const overloads. It compiles with GCC 15 in C++23 mode.
  • Ref-qualifiers (void f() &;, void f() &&;) decide whether *this is an lvalue or rvalue for overload selection.