I'm fairly new to C++, and I don't understand what the this
pointer does in the following scenario:
void do_something_to_a_foo(Foo *foo_instance);
void Foo::DoSomething()
{
do_something_to_a_foo(this);
}
I grabbed that from someone else's post on here.
What does this
point to? I'm confused. The function has no input, so what is this
doing?
Nonstatic member functions such as
Foo::DoSomething
have an implicit parameter whose value is used forthis
. The standard specifies this in C++11 §5.2.2/4:As a result, you need a
Foo
object to callDoSomething
. That object simply becomesthis
.The only difference (and it's trivial) between the
this
keyword and a normal, explicitly-declaredconst
pointer parameter is that you cannot take the address ofthis
.It is a local pointer.It refers to the current object as local object