The this
pointer inside a class is a reference to itself. It's needed for example in this case:
class YourClass
{
private:
int number;
public:
YourClass(int number)
{
this->number = number;
}
}
(while this would have been better done with an initialization list, this serves for demonstration)
In this case you have 2 variables with the same name
- The class private "number"
- And constructor parameter "number"
Using this->number
, you let the compiler know you're assigning to the class-private variable.