Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 41

this Pointer in C++

In this lesson: Use the this pointer in C++ to refer to the current object and chain methods — examples with output. In Hindi & English.

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:

C++
#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
}
Output:
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:

C++
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
}
Output:
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 of this->member (arrow) — this is a pointer.
  • Returning this instead of *this when you meant to return the object by reference.
  • Expecting this to exist in a static member function.
  • Overusing this-> where there is no name clash (it is optional then).
🏋️ Practice

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

  • this is a hidden pointer to the object a member function runs on.
  • Use this->name to tell a member from a same-named parameter.
  • Return *this to enable method chaining.
  • this is a pointer, so use the arrow operator with it.
  • Static member functions have no this pointer.

Frequently Asked Questions

What is the this pointer in C++?
The 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++?
It is used to tell a member and a parameter apart when they share a name, to return the current object so calls can be chained, and to pass the current object to another function. It makes "the object this function is running on" available by name.
What does return *this mean in C++?
Returning *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++?
No. A static member function belongs to the class rather than any object, so it has no 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.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicAccess Specifiers: public, private, protected Your progress: 41 / 81 (51%) · Lesson 42 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free