‘this’ pointer:
C++ uses a unique keyword called „this‟ to represent an object that invokes a member function. This unique pointer is automatically passed to a member function when it is invoked. „this‟ is a pointer that always point to the object for which the member function was called.
For example, the function call A.max ( ) will set the pointer „this‟ to the address of the object A. Then suppose we call B.max ( ), the pointer „this‟ will store address of object B.
Example:
#include<iostream.h>
class sample
{
int a;
public:
void setdata(int x)
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when setdata ( ) and putdata ( ) functions are called.