The this Pointer
Prerequisites: Access specifiers & encapsulation
Inside a member function, this is a pointer to the specific
object the function was called on — useful when a parameter name
shadows a member, or to return the object itself for chaining:
class Counter {
private:
int count = 0;
public:
Counter &increment() {
this->count++;
return *this; // enables chaining
}
int getCount() const {
return count;
}
};
Counter c;
c.increment().increment().increment();
std::cout << c.getCount() << std::endl; // 3