I was told that member function of a C++ class has an implicit parameter this
which is a pointer to the object. But it seems to me that this parameter should be the object, not a pointer to it. For example, the overloaded operator +
in expression obj1 + obj2
has its left operand as the first parameter of operator+
member function, which is the object obj1
instead of its address. So I would like to be confirmed whether the implicit parameter of a member function in C++ is *this
or this
.
4
this
needs to come from somewhere, thats what is usually called “the implicit (this
) parameter”. this
is a pointer, *this
is a reference to the obejct.
It is implicit, because how it is actually done under the hood is an implementation detail. As pointed out in a comment, it makes no difference whether the implementation implicitly passes the object or a pointer, what you get is this
and *this
in either case (and a pointer is of course cheaper to pass).