42 The this Keyword in C++
In C++, there is a keyword called this
, which allows access to member functions.
this
is a pointer to the current object instance, and the method belongs to this object instance.
If we don't use Member Initializer Lists (as shown in the comment), and instead write inside the method, since the parameter x
and the member x
have the same name, x = x
will only assign the passed x
to itself, effectively doing nothing.
What I really want to do is reference the x
and y
that belong to this class. The this
keyword allows us to achieve this.