this Pointer in C++

In this tutorials we will study and understand the concept and working of this Pointer in C++

this Pointer in C++
this Pointer in C++

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. this pointer can also be used to return its own reference.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.
this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
Following are the situations where ‘this’ pointer is used:

  • When local variable’s name is same as member’s name.
  • To return reference to the calling object.
When local variable’s name is same as member’s name.

Run Online

Output
x = 20
To return reference to the calling object.

Run Online

Output
Enter the Value :5
Enter the Value :8
Maximum No. is : 8

Leave a Reply

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