Home | Projects | Notes > C++ Programming > this Pointer
this Pointer
this is a reserved keyword that contains the address of the current object.
It's a pointer to the object that's currently being used by the class member methods.
Can only be used within the class scope
All member access is done via the this pointer.
C++ allows you to use member names directly. And, behind the scenes, it's actually using this pointer.
Can be used by the programmer
To explicitly access data member and methods
To determine if two objects are the same
Can be dereferenced (*this) to yield the current object
When overloading the assignment operator
Account class
xxxxxxxxxx41void Account::set_balance(double bal)2{3 balance = bal; // 'this->balance' is implied4}this pointer can be used to disambiguate identifier
xxxxxxxxxx51void Account::set_balance(double balance)2{3 // balance = balance // Ambiguous (which balance?)4 this->balance = balance; // Unambiguous5}To determine object identity
Sometimes it is useful to know if two objects are the same object
xxxxxxxxxx81int Account::compare_balance(const Account &other)2{3 if (this == &other)4 std::cout << "The same objects" << std::endl;5 ...6}7
8jack_account.compare_balance(jack_account);L3: When the compare logic is involved or computationally expensive, then a quick check to see if the objects are the same could help performance.