this Pointer
On defining a class, the member functions are created and placed in the memory space only once i.e., only one copy of member functions are maintained, that is shared by all the objects of the class. Memory space for the data members is allocated separately for each object.
This has an associated problem. If only one instance of member functions exist, how does the compiler come to know which object's data member is to be manipulated?
For example, if member-function3 is capable of changing the value of data-member2 and the user wants to change the value of data-member2 of object1. How would the member-function3 come to know which object's data member is to be changed?
The answer to this problem is this pointer. When a member function is called, it is automatically passed with an implicit argument that is a pointer to the object that invoked the function. This pointer is called this. That is if object1 is invoking member-function3, then an implicit argument is passed to member-function3 that points to object1 i.e. this pointer now points to object1.
Example:
Example: A program to illustrate the working of this pointer.
Comments - No Responses to “this Pointer”
Sorry but comments are closed at this time.