What is the ‘this’ Pointer in C++?

c++classpointersthis

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?

Best Answer

this refers to the current object.

The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a non-static member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x.

Related Question