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.
Implementation:
#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 Code
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:
Implementation:
#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 Code
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.
Implementation:
#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 Code
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:
Implementation:
#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 Code
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.
Try and compile with online c++ compiler.
Frequently Asked Questions
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;
What is the hidden this pointer in C++?
In C++, the hidden this pointer refers to an implicit pointer passed to member functions of a class. It points to the current object of the class, enabling access to its members and methods within non-static member functions.
Conclusion
In this article, this pointer in C++ is an implicit pointer automatically passed to all non-static member functions. It allows access to the current object’s members and methods, enabling object-specific behavior. It’s especially useful for distinguishing between member variables and function parameters when they share the same name.