New update is available. Click here to update.

‘this’ pointer

ABHINAV KUMAR
Last Updated: Nov 24, 2022
EASY

Introduction

C++ is the most extensively utilised programming language for competitive programming. Learning C++ reinforces essential computer science principles while also providing access to a wide range of professional options. In C++, every object has access to its own address via an important pointer known as this pointer. All member functions take this pointer as an implicit parameter. As a result, this can be used to refer to the invoking object within a member function. This blog explains the details of the ‘this’ pointer in C++ with its properties and examples.

Let's start with the details of the ‘this’ pointer.

'this' pointer

The 'this' pointer contains the address of the current object; in other words, this pointer points to the class's current object. Each object receives its copy of data members, whereas all objects share a single copy of member functions. All non-static member function calls receive the 'this' pointer as a secret parameter, and all the non-static functions have it as a local variable. The static member functions can be called without any object due to which the 'this' reference is not available (with class name).C++ lets objects destroy themselves by calling the following code :

Delete this;

'this' can be used as a reference rather than a pointer, but the reference was not available in early C++ versions. The aforementioned problem could be avoided if 'this' is implemented as a reference, and it could be safer than the pointer.

Let's take things a step further by brushing up on how to use the 'this' pointer.

Usage of ‘this’ pointer

The following are the main usage of ‘this’ pointer:

  • It can be used as a parameter to pass the current object to another method.
  • It can be used to refer to the current instance variable of a class.
  • It's possible to use it to declare indexers.

Now it's time to brush up on our skills by learning how to use the 'this' pointer through examples.

Example 

The following examples explain the implementation of ‘this’ pointer.

When the name of a local variable and the name of a member is the same.

When the name of a local variable and the name of a member is the same, you won't be able to assign the local variable value to the data members until you use this pointer because the compiler won't recognise that you're referring to the object's data members unless you use this reference.

#include<bits/stdc++.h>
using namespace std;
/* the name of a local variable and the name of a member is the same.*/
class Marks
{
private:
   int a;
public:
   void set_Marks (int a)
   {
       // The object a is retrieved via the 'this' pointer.
       this->a = a;
   }
   void show() { cout << "Marks = " << a << endl; }
};
 
int main()
{
   Marks obj;
   int x = 90;
   obj.set_Marks(x);
   obj.show();
   return 0;
}

Output :

Marks = 90

Explanation:

We have a data member ‘a’. We have a local variable with the same name as the data member's name in the member method set_Marks(). In this scenario, you won't be able to assign the local variable value to the data members until you use this pointer because the compiler won't recognise that you're referring to the object's data members unless you use this reference.

Let’s have a look at another example:

Code:

#include<bits/stdc++.h>
using namespace std;
/* the name of a local variable and the name of a member is the same.*/
class Marks
{
private:
   int a = 5000; //Default
public:
   void Wages (int a)
   {
       // The object a is retrieved via the 'this' pointer.
       this->a = a;
   }
   void show() 
   {
     cout << "Wages = " << a << endl; 
     }
};
 
int main()
{
   Marks obj;
   int x = 8000;
   obj.show(); //Prints the default value of Wages
   obj.Wages(x);
   obj.show(); //Updates Value of Wages
   return 0;
}

Output:

Wages = 5000
Wages = 8000

Explanation:

In the above code snippet, the default value of the wages is 5000. The this pointer is used to update the value of the referenced variable. Updating the values changes the value to 8000. Hence the new value gets printed.

Using this pointer to call functions in a chain

The returned reference to a local object can be used to chain function calls on the same object. Let's have a look at the following code sample to help us understand it better.

#include<iostream>
using namespace std;
class Marks
{
  private:
  int phy;
  int chem;
  public:
  Marks(int phy = 0, int chem = 0)
  {
    this->phy = phy;
    this->chem = chem;    
  }
  Marks &set_phy(int a)
  {
    phy = a;
    return *this;  
  }
  Marks &set_chem(int b)
  {
    chem = b;
  return *this;
  }
  void show()
  {
    cout << "Phy = " << phy << " Chem = " << chem << endl;
  }
};
 
int main()
{
Marks obj1(45, 55);
obj1.show();
obj1.set_phy(72).set_chem(63); //The values have been updated to a new value.
obj1.show();
return 0;
}

Output:

Phy = 45 Chem = 55
Phy = 72 Chem = 63

Explanation:

This pointer is used to return the current object's reference so that you can call all of the current object's functions at once by chaining function calls. Another thing to note in this program is that we edited the value of objects in the second function, and you can see in the output that it actually updated the value we set in the first function call. This shows that the chaining is sequential and that the changes made to the object's data members are preserved between chaining calls.

Let’s have a look at another example:

Code:

#include<iostream>
using namespace std;
class Marks
{
  private:
  int phy;
  int chem;
  int math;
  public:
  Marks(int phy = 0, int chem = 0, int math = 0)
  {
    this->phy = phy;
    this->chem = chem;
    this->math = math;      
  }
  Marks &set_phy(int a)
  {
    phy = a;
    return *this;  
  }
  Marks &set_chem(int b)
  {
    chem = b;
  return *this;
  }
  Marks &set_math(int c)
  {
    math = c;
  return *this;
  }
  void show()
  {
    cout << "Phy = " << phy << " Chem = " << chem << " Math = " << math << endl;
  }
};
 
int main()
{
Marks obj1(45, 95, 65);
obj1.show();
obj1.set_phy(92).set_chem(63).set_math(99); //The values have been updated to a new value.
obj1.show(); //It prints the new value.
return 0;
}

Output:

Phy = 45 Chem = 95 Math = 65
Phy = 92 Chem = 63 Math = 99

Explanation:

In the above program, we have defined obj1 with marks as 45 for physics , 95 for chemistry and 65 for maths. We have used a sequence of methods of Marks class to update the value of physics to 92, chemistry to 63 and maths to 99. The chaining sequence updates the marks using the this pointer and then prints the updated values.

Frequently Asked Questions

What is ‘this’ pointer?

The ‘this’ pointer holds the address of the current object; in other words, this pointer points to the class's current object.

What is the usage of this pointer?

  • It can be used as a parameter to transmit the current object to another method.
  • It can be used to refer to the current instance variable of a class.
  • It's possible to use it to declare indexers.

What is the best way to destroy the objects?

The objects can be destroyed using the following statement

delete this;

Conclusion

In this article, we have extensively discussed the ‘this’ pointer. The article explains the details and usage of the ‘this’ pointer.
We hope that this blog has helped you enhance your knowledge regarding ‘this’ pointer and if you would like to learn more, check out our articles on C++. You can refer to our guided paths on the Codestudio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Was this article helpful ?
2 upvotes