Case 1: Disambiguation

class MyClass {
    int value;
public:
    void setValue(int value) {
        this->value = value;  // "this->value" is the member; "value" is the parameter.
    }
};

Case 2: Method chaining

class MyClass {
    int x, y;
public:
    MyClass& setX(int x) {
        this->x = x;
        return *this;  // Enables chaining.
    }
    MyClass& setY(int y) {
        this->y = y;
        return *this;  // Enables chaining.
    }
};

// Usage:
MyClass obj;
obj.setX(10).setY(20);

Case 3: Self-assignment check

class MyClass {
    int data;
public:
    MyClass& operator=(const MyClass& other) {
        if (this == &other)  // Check for self-assignment.
            return *this;
        // Copy data...
        data = other.data;
        return *this;
    }
};

Case 4: Passing the current object

Sometimes you need to pass the current object’s address to other functions or methods. You can do this directly using this.

Case 5: Lambda capturing

In C++11 and later, when using lambdas inside member functions, you might capture this to access members of the current object:

class MyClass {
    int value;
public:
    void doSomething() {
        auto lambda = [this]() { 
            // Use this->value inside the lambda.
            return this->value;
        };
        int result = lambda();
    }
};