'this' pointer in C++ (original) (raw)

Last Updated : 3 Jun, 2026

In C++, the this pointer is a special pointer available inside non-static member functions that points to the object invoking the function. It helps member functions identify and work with the current object on which they are called.

**Example: The following example demonstrates how this differentiates between a data member and a constructor parameter with the same name.

C++ `

#include using namespace std;

class Student { int age; public: Student(int age) { this->age = age; // data member = parameter }

void show() {
    cout << "Age: " << age << endl;
}

};

int main() { Student s(20); s.show(); return 0; }

`

**Explanation:

Key Characteristics of the this Pointer

The this pointer has several important properties that define how it behaves and how it can be used within class member functions.

Applications of 'this' pointer

The this pointer is commonly used in member functions to work with the current object explicitly. Some of its common applications are:

applications_of_this_pointer

Applications of this pointer

Returning the Current Object Using this

Returning ***this allows **method chaining, where multiple function calls are made on the same object in a single statement.

**Syntax:

ClassName& functionName() {
return *this;
}

**Example: Method Chaining Using this

C++ `

#include using namespace std;

class Test { int x, y; public: Test(int x = 0, int y = 0) { this->x = x; this->y = y; }

Test& setX(int a) {
    x = a;
    return *this;
}

Test& setY(int b) {
    y = b;
    return *this;
}

void print() {
    cout << "x = " << x << " y = " << y << endl;
}

};

int main() { Test obj; obj.setX(10).setY(20).print(); return 0; }

`

**Explanation: