8.7 — The hidden “this” pointer

One of the big questions that new programmers often ask is, “When a member function is called, how does C++ know which object it was called on?”. The answer is that C++ utilizes a hidden pointer named “this”! Let’s take a look at “this” in more detail.


Here’s a sample program that uses this class:

Let’s take a closer look at the following line: cSimple.SetID(2);. Although it looks like this function only has one parameter, it actually has two! When you call cSimple.SetID(2);, C++ internally converts this to SetID(&cSimple, 2);. Note that this is just a normal function call where C++ has added a parameter, and automatically passed in the address of the class object!

Since C++ converts the function call, it also needs to convert the function itself. It does so like this:


C++ has added a new parameter to the function. The added parameter is a pointer to the class object the class function is working with, and it is always named “this”. The this pointer is a hidden pointer inside every class member function that points to the class object the member function is working with.

Note that m_nID (which is a class member variable) has been converted to this->m_nID. Since “this” is currently pointing to cSimple, this actually resolves to cSimple->m_nID, which is exactly what we wanted!

Most of the time, you never need to explicitly reference the “this” pointer. However, there are a few occasions where it can be useful:

1) If you have a constructor (or member function) that has a parameter of the same name as a member variable, you can disambiguate them by using “this”:

Note that our constructor is taking a parameter of the same name as a member variable. In this case, “nData” refers to the parameter, and “this->nData” refers to the member variable. Although this is acceptable coding practice, we find using the “m_” prefix on all member variable names provides a better solution by preventing duplicate names altogether!

2) Occasionally it can be useful to have a function return the object it was working with. Returning *this will return a reference to the object that was implicitly passed to the function by C++.

One use for this feature is that it allows a series of functions to be “chained” together, so that the output of one function becomes the input of another function! The following is somewhat more advanced and can be considered optional material at this point.

Consider the following class:



Note that Add(), Sub() and Mult() are now returning *this, which is a reference to the class itself. Consequently, this allows us to do the following:


First, cCalc.Add(5) is called, which adds 5 to our m_nValue. Add() then returns *this, which is a reference to cCalc. Our expression is now cCalc.Sub(3).Mult(4). cCalc.Sub(3) subtracts 3 from m_nValue and returns cCalc. Our expression is now cCalc.Mult(4). cCalc.Mult(4) multiplies m_nValue by 4 and returns cCalc, which is then ignored. However, since each function modified cCalc as it was executed, cCalc now contains the value ((0 + 5) - 3) * 4), which is 8.

Although this is a pretty contrived example, chaining functions in such a manner is common with String classes. For example, it is possible to overload the + operator to do a string append. If the + operator returns *this, then it becomes possible to write expressions like:

And it is pretty easy to see the benefit in being able to do that! We will cover overloading the + operator (and other operators) in a future lesson.

The important point to take away from this lesson is that the “this” pointer is a hidden parameter of any member function. Most of the time, you will not need to access it directly. It’s worth noting that “this” is a const pointer -- you can change the value of the object it points to, but you can not make it point to something else!

8.8 -- Constructors (Part II)
8.6 -- Destructors

30 comments to 8.7 — The hidden “this” pointer

  • rajeshsingh



  • Hesham

    Bravo, I see Good Work here!

  • MrFinn

    Darn, this page has some serious issues, it won't let me fix the line and eats some characters. The line should be, cout, then <<, then member access operator from object ptr pointing to GetValue() and then <<endl;

  • MrFinn

    Returning '*this' from the member fuctions means that you are dereferencing the object itself from the objects address 'this'. The return type from the member functions is reference type to access the actually returned object. If you want to use pointers and return 'this' from the member functions instead of '*this' you could do it like below. Note the object instantiated in the heap with 'new' and then deleted:

    #include "stdafx.h"

    class Calc
    int m_nValue;

    Calc() { m_nValue = 0; }

    Calc* Add(int nValue) { m_nValue += nValue; return this; }
    Calc* Sub(int nValue) { m_nValue -= nValue; return this; }
    Calc* Mult(int nValue) { m_nValue *= nValue; return this; }

    int GetValue() { return m_nValue; }


    int main()
    using namespace std;
    Calc *cCalc= new Calc;


    delete cCalc;


    return 0;


    I would like to comment on ampersand after return type.

    returning *this is used to achieve function chaining i.e. we want to call functions on the same objects.
    for ex. if you check operator overloading of << insertion operator it return input reference again

    ostream& operator<< (ostream &out, Point &cPoint)

    out << "(" << cPoint.m_dX << ", " <<
    cPoint.m_dY << ", " <<
    cPoint.m_dZ << ")";
    return out;
    cout << pointobj1 << pointobj2;

    so function call goes like this
    1)cout.operator <<(pointobj1);// in operator << we are returning reference of cout
    //that is used for calling << 2nd time
    2)cout.operator <<(pointobj2);

    In your code after removing & from return type from Add, Mul, Sub etc.
    return type is temporary object.

    your code:
    cout<<cCalc.GetValue(); gives you 5 instead of 8.
    so in short you need to ensure all functions should be called on same cCalc object. you have to return it by reference and not by value.

    I hope it helps...

  • j.howard








    Any help understanding this would be much appreciated.


    • j.howard







  • priyesh lakar


Leave a Reply

You can use these HTML tags
