SatView: Pointer Perfect, Part 2: Construction / Destruction - The This Pointer
(Page 2 of 4 )
Every object in C++ has a hidden parameter also known as the this pointer. It points to the instantiated class (object) and was therefore appropriately named "this". The compiler inserts this pointer implicitly in your code when you are accessing members and functions of an object.
MyClass::foo() {
myVar = 20;
}
can therefore be read as
MyClass::foo() {
this->myVar = 20;
}
You will also encounter the this pointer when writing the assignment operator for a class:
MyClass& MyClass::operator =(MyClass const &rhs) {
if (&rhs != this)
myVar = rhs.myVar;
return *this;
}
Note how the reference operator on the this pointer used in the return statement, returns a reference of that very same object!
Next: Copying Pointers vs. Copying Memory >>
More Code Examples Articles
More By J. Nakamura