The Pointer this


When a class is defined, its member functions are stored away in memory to be used by any of the class' objects. The member functions are not actually part of the objects themselves but are accessible by any of them depending on the access section of the member function. To enable access, every member function has an implicit argument that is the pointer: this. When a member function is called by a class object, the pointer variable this is passed automatically to the function by the compiler. The pointer variable: this is actually a constant pointer to the class and which contains the address of the class object that called the member function. For example see this.cpp.

The following shows two logically equivalent member functions definitions. The first member function is used with the class Circle where the function getRadius() is a member function. The second one is used with a class Circle where the function getRadius() is a member function with an explicit use of the this pointer.

image from book

 void Circle::getRadius(double theInput) {   radius = theInput; } void Circle::getradius(double theInput) {    this->radius = theInput; } 

image from book

Remember that the class pointer this is a constant and therefore while its object can have its value changed, however the value of this (i.e. the address of the class object to which it points) can not be changed during its lifetime. See circles.cpp. In this example, these two functions have accomplished the same thing. The pointer this is actually pointing to an object of the class Circle which is how C++ handles class member functions. The function getRadius() has the expression this->radius. The this-> is not required in this case because the compiler writes the code automatically.

In general the C++ programmer does not need to know about the pointer: this. However there may be times in which you may want to use this. For example you may want to access the object that called the member function from inside of the function. Of course the programmer could not code for multiple objects before hand. That is, if the object stuff1 called the function one time and stuff2 called it the second time, it would not be possible for the programmer to code for both instances. You could access the calling object by using code like:

image from book

 return(*this); 

image from book

Let's consider an example that demonstrates the use of this. In the example: futrpas2.cpp, the operator ++() is overloaded. (In a later section of the lectures, the concept of operator overloading will be discussed.) We want this operator to not only increment the Date object but also to return the value of the object so that it could be stored into another object. That is:

image from book

 ++today; 

image from book

should not only change the value of the object today, but we should be able to place this value into the object tomorrow as in the following:

image from book

 tomorrow = ++today; 

image from book

When this was done in the above example, a Date object temp was created in the body of the operator's definition to provide for this transfer in the statement:

image from book

 return temp; 

image from book

But this could have been done without the object temp by using the pointer this as in the following:

image from book

 return *this; 

image from book

For example see newdate.cpp.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net