this pointer In C++

When a member function is called using an object, the address of the object is implicitly passed to the member function as the first argument (by the compiler) The member function collections this address in a pointer named this.

Simple Example of this Pointer In C++

#include<iostream> using namespace std; class This_Pointer{ int a; public: void setData(int a){ this->a = a; } void printData(){ cout<<"The value of a is"<<a<<endl; } }; int main(){ This_Pointer tp; tp.setData(10); tp.printData(); return 0; }


The value of a is 10

Explanation of the program

In this program, the 'a' is a private member of the class This_Pointer. Also, the arguments received by the member function printData() is also a. Hence, if we do not use this pointer explicitly both the 'a' will be considered as data members.