C++ this Pointer
In C++, this is a special pointer available inside non-static class methods. It points to the object that is currently running the method.
Most of the time you do not need to write this yourself, because C++ already knows which object’s members a method is using. It becomes useful when you need to be explicit.
The Current Object
When you call a method with dot syntax, such as first.print();, the method runs on one specific object. Inside that method, this points to that object.
Because this is a pointer, you use the arrow operator -> to access members through it. For example, this->name means the name member of the current object.
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int grade;
public:
Student(std::string name, int grade) {
this->name = name;
this->grade = grade;
}
void print() const {
std::cout << this->name << ": " << this->grade << std::endl;
}
};
int main(void) {
Student first("Ava", 92);
Student second("Liam", 85);
first.print();
second.print();
return 0;
}
Output:
Ava: 92
Liam: 85
The constructor parameters are named name and grade, just like the private member variables. Inside the constructor, this->name clearly means the member variable, while plain name means the parameter.
Why It Is A Pointer
The word this stores the address of the current object. That is why its type is a pointer to the class. Inside a Student method, this behaves like a Student*.
You usually do not print or store this in beginner code. The important idea is that each method call receives a hidden way to know which object it is working on.
Implicit And Explicit Member Access
In many methods, grade = 100; and this->grade = 100; mean the same thing when grade is a member variable and no local name hides it.
The first form is shorter and is common when there is no name conflict. The second form is useful when a parameter or local variable has the same name as a member variable.
Some teams prefer member names such as studentName, m_name, or name_ to avoid repeated use of this->. The exact naming style varies, but the meaning of this stays the same.
Returning The Current Object
You can dereference this with *this to refer to the current object itself. A common use is returning *this by reference from methods that update an object, which allows method calls to be chained.
#include <iostream>
#include <string>
class Player {
private:
std::string name;
int score;
public:
Player(std::string name) {
this->name = name;
this->score = 0;
}
Player& addPoints(int points) {
this->score += points;
return *this;
}
void print() const {
std::cout << name << " scored " << score << " points." << std::endl;
}
};
int main(void) {
Player player("Mina");
player.addPoints(10).addPoints(5).addPoints(3);
player.print();
return 0;
}
Output:
Mina scored 18 points.
The addPoints() method changes the current object, then returns *this. Because it returns a Player&, the next addPoints() call continues working on the same object.
Const Methods And this
In a const method, the current object is treated as const. That means a method such as print() const can read members through this, but it cannot change them.
This is why print() const can display name and score, while addPoints() is not marked const. Adding points changes the object.
Common Mistakes
- Writing
this.nameinstead ofthis->name. Sincethisis a pointer, use->. - Using
thisin astaticmethod. Static methods belong to the class, not to one specific object. - Returning
*thisby value when you meant to chain changes on the same object. - Adding
this->everywhere when there is no name conflict and the shorter form is clear.
Takeaway: this points to the current object inside a class method, and it is most useful for resolving name conflicts and returning the current object from methods.
