Hello friends, in this post today, we learn about 'this' pointer in c++ and what is the use of 'this' pointer. so lets start...
'this' pointer:-
The this pointer is a special pointer which is a built-in pointer. and 'this' is a keyword.
It stores the address of current object in context. That is the current object which can be referred using this pointer anywhere in the class.
Important Points About this Pointer:-
The this pointer can be used only inside the class i.e., only inside the member function of the class and cannot be used outside and static member of the class.
The object’s address is available from within the member function as the this pointer.
It is legal, through unnecessary, to use this pointer when referring to members of the class.
The expression (*this) is commonly used to return the current object from a member function.
this is a local object pointer in every instance member function containing address of the caller object.
‘this’ pointer represent an object that invoke or call a member function. It will point to the object for which member function is called.
The this pointer is a constant pointer.
When a non static member’s function is called for an object, the address of the object is passed as a hidden argument to the function.
It is automatically passed to a member function when it is called.
It is also called as implicit argument to all member function.
Example:
Output:
The most important advantage of ‘this’ pointer is, If there is same name of argument and data member than you can differentiate it.
By using ‘this’ pointer we can access the data member and without ‘this’ we can access the argument in same function.
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment