What is Advantage and Use of THIS pointer in C++ – Scenarios?

(Last Updated On: March 4, 2017)

Answer includes use and advantage of this pointer in C++ programming with multiple scenarios where THIS pointer is used.

Sceranio-1: Internal use of this pointer.

Wherever an object calls a class member function then compiler internally pass a THIS pointer to member function internally. For example, in below class, we have a Set model member function that receives one argument i.e. car model.

In fact, besides model parameter one more argument is passed to the set model function internally that is THIS pointer.




Scenario-2: Make class member fields and member function parameter readable.

One advantage of this pointer in C++ program is to make constructor or function parameter and class data member readable. In below class, member variable “ modle” and parameter “modle” in function SetModel(int model)are of same name.So, to make these items readable and unambiguous to program, we use this pointer. Example below. However note that even though if we write model=model in SetModel function instead of this->model = model; program will work fine.