The this Pointer


To understand the this pointer, you need to understand the following things:

  1. Each C++ object gets its own copy of each of the non-static data members of the class. (These data members are sometimes referred to as instance variables since they are a property of each instance / object.)
  2. All objects of a class share a single copy of each static data member of the class. (These data members are sometimes referred to as class variables since they are a property of the class as a whole rather than a particular object of the class.)
  3. A single copy of each compiled member function definition is stored in the code segment of the C++ program.

If only one copy of each member function definition exists and is used by multiple objects, how does the code in that member function access the non-static data members for the correct object (i.e., the one that called the member function)?

The compiler supplies a special implicit pointer named this that allows a member function to access the non-static data members. The this pointer contains the address of the object that called the member function.

The this pointer is passed as an extra, hidden argument whenever a non-static member functions is called and is available as a local variable within the body of all non-static member functions. The this pointer is not available in static member functions since they can be called using a class name rather than the name of a specific object.

Date d1(6, 12, 2010);
Date d2(3, 23, 2016);
                
d1.set_month(8);       // Passes &d1 as the this pointer for set_month().
d2.set_year(2019);     // Passes &d2 as the this pointer for set_year().

Here are some examples of how to use the this pointer:

this                     // Address of the object that called the member function.

*this                    // Value of the object that called the member function.

(*this).member_name       // Syntax to access a data member of the object that called the member function.

this->member_name         // Alternative syntax to access a data member.

(*this).member_function_name(arguments)      // Syntax to call another member function using the
                                           // object that called this member function.
                                                               
this->member_function_name(arguments)        // Alternative syntax to call a member function.

The this pointer can not be modified, so assignments to this are not allowed.

For a class named MyClass, the data type of the this pointer is MyClass* (pointer to MyClass). If a member function of MyClass is declared as const, then the data type of the this pointer will instead be const MyClass* (pointer to a MyClass constant). That prevents the this pointer from being used to change the data members of the object to which it points.

Most uses of the this pointer inside a member function's body will be implicit. It is legal, though usually unnecessary, to explicitly use this when referring to data members or calling methods of the class. For example:

void Date::set_month(int new_month)
{
    // The following three statements are equivalent.
    month = new_month;
    this->month = new_month;
    (*this).month = new_month;
}

If you have a local variable in a member function that has the same name as a data member of the class, using the this pointer explicitly will allow the compiler to tell them apart:

void Date::set_month(int month)
{
    this->month = month;     // Assign the local variable named month to the data member named month.
}

Two other common uses for the this pointer are to check for self-assignment and to return a reference to the object that called a member function:

MyClass& MyClass::operator=(const MyClass& other)
{
    // If the object that called the member function and the object other have the same
    // address, then they are the same object.
    if (this != &other)
    {
        // Code to execute if this does not point to the object other.
        ...
    }
    
    // Return a reference to the object that called the function.
    return *this;
}