Loading...

What is the this Pointer in C++?

Learn how the this pointer works in C++, including object self-reference, method chaining, and class design techniques.

In C++, every class member function (method) implicitly receives a this pointer parameter. The this pointer represents the address of the object for which the function is called. In other words, within a class method, this can be used to access the current object being manipulated.


1. What is the this Pointer?

The this pointer is an implicit pointer automatically passed to each class method. Every non-static member function accesses the address of the object it operates on through this pointer.


#include <iostream>
using namespace std;

class Book {
private:
    string title;

public:
    Book(string t) {
        this->title = t; // using this
    }

    void Show() {
        cout << "Book: " << this->title << endl;
    }
};

int main() {
    Book b("Learning C++");
    b.Show();
}

Here, this->title accesses the title member of the current object. Writing title = t; would have the same effect, but this is especially useful when parameter names conflict with member names.


2. When Parameter Names Conflict with Member Names

In the following example, the constructor’s parameters have the same names as the class members. In this case, using this is required to avoid ambiguity.


class Book {
private:
    string title;
    double price;

public:
    Book(string title, double price) {
        this->title = title; // without "this" it would assign parameter to itself
        this->price = price;
    }

    void Show() const {
        cout << "Book: " << title << " | Price: " << price << " USD" << endl;
    }
};

int main() {
    Book b("Modern C++", 120.5);
    b.Show();
}

The expression this->title refers to the class’s title member. Without this, the compiler would be unable to distinguish it from the local parameter.


3. Memory Address of the this Pointer

Each object has a different this address because every instance resides at a unique location in memory.


#include <iostream>
using namespace std;

class Book {
private:
    string title;
public:
    Book(string t) : title(t) {}

    void PrintAddress() const {
        cout << "Object address (this): " << this << endl;
    }
};

int main() {
    Book b1("C++ Primer");
    Book b2("Effective C++");

    b1.PrintAddress();
    b2.PrintAddress();
}

The output will be different for each object because this stores each object’s unique address.


4. Method Chaining

The this pointer can be returned from methods to enable method chaining. This allows multiple method calls to be executed in a single statement.


#include <iostream>
#include <string>
using namespace std;

class Book {
private:
    string title;
    double price;
    int pageCount;

public:
    Book() : title(""), price(0), pageCount(0) {}

    Book* SetTitle(string t) {
        this->title = t;
        return this; // returns its own address
    }

    Book* SetPrice(double p) {
        this->price = p;
        return this;
    }

    Book* SetPageCount(int c) {
        this->pageCount = c;
        return this;
    }

    void Show() const {
        cout << title << " (" << pageCount << " pages) - " 
             << price << " USD" << endl;
    }
};

int main() {
    Book b;
    b.SetTitle("Object-Oriented Programming with C++")
     ->SetPrice(149.90)
     ->SetPageCount(580)
     ->Show();
}

This technique is known as method chaining and is commonly used in Builder or Fluent Interface design patterns.


5. Operator Overloading with this Pointer

The this pointer is also frequently used in operator overloading. For example, adding two Book objects based on their prices:


#include <iostream>
#include <string>
using namespace std;

class Book {
private:
    string title;
    double price;

public:
    Book(string t, double p) : title(t), price(p) {}

    // Operator Overloading (+)
    Book operator+(const Book &other) const {
        double totalPrice = this->price + other.price;
        return Book("Bundle: " + this->title + " & " + other.title, totalPrice);
    }

    void Show() const {
        cout << title << " - " << price << " USD" << endl;
    }
};

int main() {
    Book b1("C++ Primer", 180.0);
    Book b2("Effective Modern C++", 220.0);

    Book combo = b1 + b2;
    combo.Show();
}

Here, this->price represents the price of the left-hand operand in the operation. This is useful for combining or comparing multiple objects.


6. Using this in Constructors

Within constructors, this can be used to refer to the object’s address before initialization is complete. It is often used for logging or debugging purposes.


class Book {
public:
    Book() {
        cout << "Creating new Book object... Address: " << this << endl;
    }
};

This is particularly helpful in complex object hierarchies (e.g., Book → Author → Publisher) to track object creation and destruction during runtime.


7. this Cannot Be Used in Static Functions

Static functions belong to the class itself, not to any specific object. Therefore, the this pointer does not exist within static functions.


class Book {
public:
    static void Info() {
        // cout << this; // ERROR! "this" cannot be used in a static function
        cout << "This is a static function." << endl;
    }
};

8. TL;DR

  • The this pointer represents the address of the current object.
  • this->member → used to access that object’s data members.
  • When parameter names conflict with member names, this is required.
  • return this; → enables method chaining.
  • this can also be used in operator overloading.
  • this is not available inside static functions.
  • All examples can be compiled and executed in Visual Studio 2022 or GCC 11+ environments.

Related Articles