Can you please explain the use of this pointer?

Submitted by: Administrator
When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object (ie the object on which the function is invoked). This pointer is known as this pointer. It is internally created at the time of function call.

The this pointer is very important when operators are overloaded.

Example: Consider a class with int and float data members and overloaded Pre-increment operator ++:

class MyClass
int i;
float f;
MyClass ()
i = 0;
f = 0.0;
MyClass (int x, float y)
i = x; f = y;
MyClass operator ++()
i = i + 1;
f = f + 1.0;
return *this; //this pointer which points to the caller object
MyClass show()
cout<<”The elements are:n” cout<i<<”n<f; //accessing
data members using this

int main()
MyClass a;
++a; //The overloaded operator function ++()will return a's this
retun 0;
The o/p would be:
The elements are:
Submitted by: Administrator

Read Online C++ Pointers & Functions Job Interview Questions And Answers