Usage of ‘this’ Pointer in C++
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 of “this” Pointer in C++
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.
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;
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;
}
You can also try this code with Online C++ Compiler
Run CodeOutput :
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;
}
You can also try this code with Online C++ Compiler
Run CodeOutput:
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;
}
You can also try this code with Online C++ Compiler
Run CodeOutput:
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;
}
You can also try this code with Online C++ Compiler
Run CodeOutput:
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.
Try and compile with online c++ compiler.
Must Read Dynamic Binding in C++
Also See - Difference between argument and parameter
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 Coding Ninjas Studio 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 problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.