CppThe this Pointer

The this Pointer

Every non-static member function in C++ has access to an implicit pointer named this, which points to the specific object the function was called on. You never declare it — the compiler passes it in silently — but you can use it explicitly whenever it helps.

this is implicit

CPP
class Rectangle {
public:
    double width, height;

    double area() const {
        // These two lines are equivalent:
        return width * height;
        // return this->width * this->height;
    }
};
Disambiguating from a parameter with the same name

A very common use of this is inside a constructor or setter when the parameter name matches the member name — a style many programmers prefer because it avoids inventing a second name for the same concept.

this->member disambiguates from a same-named parameter

CPP
class Rectangle {
public:
    double width, height;

    void setWidth(double width) {
        this->width = width; // this->width is the member; width is the parameter
    }

    void setHeight(double height) {
        this->height = height;
    }
};
Method chaining with *this

A member function can return *this — the current object, by reference — to allow the caller to chain multiple calls together in a single expression. This is the basis of the “fluent interface” style seen throughout the standard library (for example, chained << on std::cout).

A fluent interface using *this

CPP
#include <iostream>

class TextBuilder {
public:
    TextBuilder& add(const std::string& word) {
        text += word;
        return *this; // return a reference to the current object
    }

    TextBuilder& addSpace() {
        text += " ";
        return *this;
    }

    std::string text;
};

int main() {
    TextBuilder b;
    b.add("Hello").addSpace().add("world").addSpace().add("!");

    std::cout << b.text << "\n"; // Hello world !
}

Because add() and addSpace() return a reference to the same object they were called on, each call can be immediately followed by another call on the result — that's what makes the chain b.add(...).addSpace().add(...) compile and do what it looks like it does.

this in const member functions
Note
Inside a `const` member function, `this` has type `const ClassName*` rather than `ClassName*` — a pointer to a *constant* object. That's precisely what enforces the promise a `const` method makes: it cannot modify any member variable through `this`, because the compiler treats everything reached via `this` as read-only in that context.

const methods cannot modify members via this

CPP
class Rectangle {
public:
    double width, height;

    double area() const {
        // this has type: const Rectangle*
        // width *= 2; // Error: cannot assign to a member through a const 'this'
        return width * height; // reading is fine
    }
};
What's next
  • Method chaining and disambiguation both rely on member functions being able to see the whole object — the next few pages (Encapsulation, Access Specifiers) cover how to control which parts of that object outside code is even allowed to see.