I've read somewhere that the "this" keyword is a default parameter (I suppose it's invisible or something) in any method of a class. Is this true?
-
3Simple answer : yes. For code generation, it is very similar to having an extra parameter `ClassName * const this`. However, usage is different as you don't have to write `this->` to access members while inside a member function. – Phil1970 Feb 04 '22 at 23:14
-
https://stackoverflow.com/questions/16492736/what-is-the-this-pointer – jxh Feb 04 '22 at 23:15
-
2As a neat bit of trivia, the ability to add an _explicit_ object parameter to member functions is currently part of a proposal for C++23. See [P0847R7](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0847r7.html). – Brian61354270 Feb 04 '22 at 23:17
-
And: https://stackoverflow.com/questions/41667653/does-every-c-member-function-take-this-as-an-input-implicitly – jxh Feb 04 '22 at 23:21
-
4`this` is often implemented as a hidden parameter, but technically how `this` gets into the method doesn't matter. You could teleport it in with Elf magic and still have a compliant implementation. – user4581301 Feb 04 '22 at 23:32
-
@Brian I am not sure that level of complexity is needed to achieve the goal. Why not allow a method to be defined like: `auto & foo () auto { ... }`. Then the method is `const` or not, returns a `const` reference or not. `foo` has to call other `auto` or `const` methods. – jxh Feb 05 '22 at 00:03
2 Answers
You can think of it that way; i.e. when you make a method call
myObject.Foo(1, 2, 3);
and the method Foo(int a, int b, int c)
executes, the code within the method has access to parameters a
, b
, and c
, and also access to the pointer this
:
void MyObjectClass :: Foo(int a, int b, int c)
{
printf("a=%i b=%i c=%i this=%p\n", a, b, c, this);
}
... this
isn't explicitly listed in the arguments list but it's passed as part of the method-call (since without it, Foo
wouldn't have access to any of its own member-variables, which would be a problem for most methods)

- 70,199
- 15
- 131
- 234
"default parameter" is the wrong term. this
can be thought of as an implicit paramter passed to member functions. If there were no member functions then you could emulate them with free functions like this:
struct Foo {
int x = 0;
};
void set_x(Foo* THIS, int x) {
THIS->x = x;
}
However, member functions do exists and the above can be written as:
struct Foo {
int x = 0;
void set_x(int x) {
this->x = x;
}
};
this
is not passed to Foo::set_x
explicitly. Nevertheless, inside the method you can use it to refer to the current object. It can be said to be an implicit parameter to the member function.
However, thats not the whole story. In member functions you actually do not need this
to refer to members of the class. Instead of this->x
just x
would work as well and in contrast to other languages it is common style to ommit the this->
unless necessary. For more details I refer you to https://en.cppreference.com/w/cpp/language/this.
Note that it is not an implicit parameter to all methods of a class. You cannot refer to the current object via this
in a static member function, because there is no "current object" in a static member function.
Also compare to python where self
is explicitly passed:
class Foo:
def __init__(self):
self.x = 0
def set_x(self,x):
self.x = x

- 109,796
- 11
- 89
- 185
-
Indeed the name given the `this` parameter in the standard is the _implicit object parameter_. From 16.3.1/2: _"a member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called"_ – Brian61354270 Feb 04 '22 at 23:21