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.
- Access and modify the data members and member functions of the current object.
- Resolve naming conflicts when function parameters and class data members have the same name.
- Return the current object from a member function to enable method chaining and fluent interfaces.
Example: The following example demonstrates how this differentiates between a data member and a constructor parameter with the same name.
#include <iostream>
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;
}
Output
Age: 20
Explanation:
- this->age refers to the data member
- age refers to the constructor parameter
- Without "this", the assignment would be ambiguous
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.
- this is an implicit pointer automatically available in all non-static member functions.
- It points to the current object that invoked the member function.
- In a class X, the type of this is X* (or const X* inside const member functions).
- It can be used to resolve naming conflicts between data members and function parameters.
- It enables method chaining by returning *this from member functions.
- It can be passed as an argument when the current object needs to be provided to another function.
- Operations such as delete this are possible but should generally be avoided unless object lifetime is carefully managed.
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:

- Resolving Name Conflicts: Distinguishes class data members from function parameters when both have the same name.
- Enabling Method Chaining: Returns the current object using *this, allowing multiple member functions to be called in a single statement.
- Passing the Current Object: Provides a way to pass the current object as an argument to another function or method.
- Operator Overloading: Used in overloaded operators to access and manipulate the current object involved in the operation.
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
#include <iostream>
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;
}
Output
x = 10 y = 20
Explanation:
- setX() and setY() return *this
- Enables chaining: obj.setX(10).setY(20)
- Improves readability and fluency of code