3 ``this''

Let us consider a alternate (unnecessarily clumsy, but is still correct) implementation of Y::init. It can be defined as follows:

void Y::Init(void)
{
  this->i = 0;
  this->j = 0;
}

The C++ compiler does not complain that this is not defined! As it turns out, this is an implicit parameter that is passed to each and every member function of a class. In this particular case, the type of this is Y * (a pointer to Y). The pointer is initialized to point to whatever object specifies the member function. In our case, myY.init() means that this is a pointer to myY.



Copyright © 2006-09-12 by Tak Auyeung