The this pointer in C++ is an implicit pointer available inside every non-static member function. It holds the address of the object the function was called on, so this->x and x refer to the same member unless a local name hides it. You use it to resolve name clashes, to return the current object for chaining, and to pass the object to other code.
Because this is a prvalue (a temporary value, not a variable), you cannot assign to it or take its address. Its type depends on the member function: T* in a normal function and const T* in a const member function.
Every non-static member function receives a hidden first argument, the address of the object. When you write obj.print(), the compiler effectively calls print(&obj), and inside the function this is that hidden argument. Static member functions have no object, so this does not exist there.
| Context | Type of this |
|---|---|
| Non-const member function | T* |
const member function | const T* |
volatile member function | volatile T* |
| Static member function | Not available |
Using this->member is optional in most cases. The compiler adds it for you when you write member alone, except inside templates with dependent base classes, where you must write this->member explicitly.
When a parameter has the same name as a data member, the parameter hides the member. Writing this->name selects the member explicitly.
In the constructor's member initializer list, name(std::move(name)) works without this because the name outside the parentheses is always the member and the name inside is the parameter. In rename, the body needs this->name because the plain name would refer to the parameter.
A member function that returns *this by reference lets callers chain calls on the same object. This is the idiom behind fluent builders and the stream insertion operators.
Return a reference (Rectangle&), not a copy. Returning by value would copy the object, and later calls in the chain would modify the copy instead of the original.
Assignment operators return *this by convention so that a = b = c works. They also use this to detect self-assignment.
The comparison this == &other avoids freeing the buffer that is about to be copied. In new code, prefer std::vector or the copy-and-swap idiom so you do not manage memory by hand.
In a const member function, this has type const T*, so the function cannot modify non-mutable members. You can overload a function on constness, and the compiler picks the version that matches the object.
Calling grow() on a const Circle object is a compile error because it needs a non-const this.
Sometimes an object has to register itself with another component, such as a callback list or an observer. You pass this for that.
Be careful when you store this beyond the object's lifetime or hand it out from a constructor before the object is fully built. If an object needs to give out shared ownership of itself, derive from std::enable_shared_from_this and call shared_from_this() instead of creating a second shared_ptr from this.
C++23 added "deducing this": a member function can declare its object as an explicit first parameter named this. This removes the need to write const and non-const overloads separately.
Compiler support is good in current GCC, Clang and MSVC releases, but check your toolchain before relying on it.
No. A static member function is not called on an object, so there is no this. Pass an object as a parameter if the function needs one.
It is not wrong, but it is usually noise. Use it when it removes ambiguity, in templates with dependent base classes, or when your project style requires it.
Syntactically yes, and it is legal if the object was allocated with new and you never touch a member afterwards. It is rare and fragile, so prefer smart pointers and clear ownership.
this is the pointer to the current object, and *this is the object itself. Return *this by reference to chain calls, and compare this with &other to detect self-assignment.
The this pointer is the hidden argument that tells a non-static member function which object it works on. Use this-> to disambiguate names, return *this by reference for chaining and assignment operators, and pass this carefully when an object registers itself elsewhere. Remember that this is const T* in const member functions, does not exist in static functions, and can be written as an explicit parameter in C++23.