Examining member function internals; the “this” pointer
So far, we have noticed that member functions are invoked through objects. We have noticed that in the scope of a member function, it is the data members (and other member functions) of the particular object that invoked the function that may be utilized (in addition to any input parameters). Alas, how, and why does this work?
It turns out that most often, member functions are invoked through objects. Whenever a member function is invoked in this fashion, that member function receives a pointer to the instance that invoked the function. A pointer to the object calling the function is then passed as an implicit first argument to the function. The name of this pointer is this.
Though the this
pointer may be referred to explicitly in the definition of each such member function, it usually is not. Even without its explicit use, the data members utilized in the scope of the function belong to this
, a pointer to...