this Pointer
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is the this pointer?
Inside every non-static member function there is a hidden pointer called this, which points to the object the function was called on. When you write obj.method(), inside method the pointer this holds the address of obj.
Disambiguating names
The most common use: when a parameter has the same name as a data member, this-> makes clear which one you mean:
#include <iostream>
using namespace std;
class Box {
int width;
public:
void setWidth(int width) {
this->width = width; // member = parameter
}
int getWidth() { return width; }
};
int main() {
Box b;
b.setWidth(50);
cout << b.getWidth(); // 50
}50
Here this->width is the member; plain width is the parameter.
Returning *this for chaining
If a member function returns *this (the current object by reference), you can chain calls one after another:
class Builder {
int x = 0, y = 0;
public:
Builder& setX(int v) { x = v; return *this; }
Builder& setY(int v) { y = v; return *this; }
void show() { cout << x << "," << y << "\n"; }
};
int main() {
Builder b;
b.setX(3).setY(7).show(); // chained calls
}3,7
Each setter returns the same object, so the next call continues on it.
Comparing objects
this also lets a function compare the current object with another, for example to check for self-assignment: if (this == &other) return *this;.
this and static functions
A static member function has no this pointer, because it is not tied to any object. That is why a static function cannot use per-object data directly.
Common mistakes
- Using
this.member(dot) instead ofthis->member(arrow) —thisis a pointer. - Returning
thisinstead of*thiswhen you meant to return the object by reference. - Expecting
thisto exist in a static member function. - Overusing
this->where there is no name clash (it is optional then).
Write a Rectangle class with setWidth and setHeight that each return *this, so you can write r.setWidth(4).setHeight(5). Add an area() and print the result of the chained calls.
Summary
thisis a hidden pointer to the object a member function runs on.- Use
this->nameto tell a member from a same-named parameter. - Return
*thisto enable method chaining. thisis a pointer, so use the arrow operator with it.- Static member functions have no
thispointer.
Frequently Asked Questions
What is the this pointer in C++?
this pointer is a hidden pointer available inside every non-static member function that points to the object the function was called on. It lets a member function refer to the current object explicitly, for example as this->name or by returning *this.Why is the this pointer used in C++?
What does return *this mean in C++?
*this returns a reference to the current object itself. This enables method chaining, where several calls are written in a row like obj.setX(1).setY(2), because each function hands the same object back to the next call.Does a static member function have a this pointer in C++?
this pointer and cannot refer to per-object members through it. Only non-static member functions receive a this pointer.What is the difference between this and *this in C++?
this is a pointer to the current object, so you use it with the arrow operator like this->member. *this dereferences that pointer to give the object itself, which is what you return when you want to return the current object by reference.this pointer क्या है?
हर non-static member function के अंदर एक छिपा pointer होता है जिसे this कहते हैं, जो उस object पर इशारा करता है जिस पर function call हुआ। जब आप obj.method() लिखते हैं, method के अंदर this pointer obj का address रखता है।
नाम स्पष्ट करना
सबसे आम इस्तेमाल: जब किसी parameter का नाम data member जैसा ही हो, this-> स्पष्ट करता है कि आप किसका मतलब रखते हैं:
#include <iostream>
using namespace std;
class Box {
int width;
public:
void setWidth(int width) {
this->width = width; // member = parameter
}
int getWidth() { return width; }
};
int main() {
Box b;
b.setWidth(50);
cout << b.getWidth(); // 50
}50
यहाँ this->width member है; सादा width parameter है।
Chaining के लिए *this लौटाना
अगर कोई member function *this (वर्तमान object reference से) लौटाए, आप calls को एक के बाद एक chain कर सकते हैं:
class Builder {
int x = 0, y = 0;
public:
Builder& setX(int v) { x = v; return *this; }
Builder& setY(int v) { y = v; return *this; }
void show() { cout << x << "," << y << "\n"; }
};
int main() {
Builder b;
b.setX(3).setY(7).show(); // chained calls
}3,7
हर setter वही object लौटाता है, तो अगली call उसी पर जारी रहती है।
Objects की तुलना
this किसी function को वर्तमान object की दूसरे से तुलना भी करने देता है, जैसे self-assignment जाँचने को: if (this == &other) return *this;।
this और static functions
Static member function के पास कोई this pointer नहीं होता, क्योंकि यह किसी object से बँधा नहीं। इसीलिए static function per-object data सीधे इस्तेमाल नहीं कर सकता।
आम गलतियाँ
this->member(arrow) के बजायthis.member(dot) इस्तेमाल करना —thisएक pointer है।- Object को reference से लौटाने का मतलब होने पर
*thisके बजायthisलौटाना। - Static member function में
thisके होने की उम्मीद करना। - जहाँ नाम टकराव न हो वहाँ
this->का अति-इस्तेमाल (तब यह वैकल्पिक है)।
एक Rectangle class लिखें जिसमें setWidth और setHeight हर एक *this लौटाएँ, ताकि आप r.setWidth(4).setHeight(5) लिख सकें। एक area() जोड़ें और chained calls का परिणाम print करें।
सारांश
thisउस object का छिपा pointer है जिस पर member function चलता है।- Member को same-नाम parameter से अलग बताने को
this->nameइस्तेमाल करें। - Method chaining सक्षम करने को
*thisलौटाएँ। thisएक pointer है, तो इसके साथ arrow operator इस्तेमाल करें।- Static member functions के पास
thispointer नहीं होता।