The this Pointer in C++
Inside any non-static member function, this is a pointer to the object the call is running on — this->balance names the caller's balance explicitly. Most code never writes it (members resolve implicitly), but three idioms need it: disambiguating parameters that shadow members, returning *this for method chaining, and passing the object's own address outward.
this is a prvalue pointer (type T* inside T, const T* inside const methods) — you can't reseat it, and using it after the object dies is the usual dangling-pointer story in miniature.
Implicit object, explicit pointer
Every non-static method secretly receives its object; this names it — balance and this->balance are the same storage.
Shadow rescue
With a parameter named like the member (name = name; assigns to itself!), this->name = name; targets the member correctly.
Chaining via *this
Returning *this by reference (Builder& setX(...) { ...; return *this; }) enables a.setX(1).setY(2); call chains.
Const-correct flavor
In const methods this is const T*, so mutation attempts fail — the compiler enforces read-only access automatically.
#include <iostream>#include <string> class Dog {public: std::string name; Dog& rename(const std::string& name) { this->name = name; return *this; }}; int main() { Dog d; d.rename("Rex").rename("Max"); std::cout << d.name << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.