‘this’ pointer in C++

  • C++ uses a unique keyword called “this” to represent an object that invokes a member function.
  • this‘ is a pointer that points to the object for which this function was called.
  • This unique pointer is called and it passes to the member function automatically.
  • The pointer this acts as an implicit argument to all the member function.

For Example:-

class demo
{
int i ;
-----
-----
};

The private variable ‘i’ can be used directly inside a member function, like
i=50;
We can also use the following statement to do the same job.
this → i = 50

class student
{
int i;
public:

void setvalue (int i)
{
this → i = i;  //here this pointer is used to assign a class level ‘i’ with the argument ‘i’

}

void display( )
{

cout << i;
}
};

main ( )
{

student S1, S2;

S1.setvalue (10) ;
S2.display ( );
}


o/p = 10

Leave a Reply

Your email address will not be published. Required fields are marked *