|
|
|
|
|
|
|
and the details of the memory management are hidden in the implementation of the classas they should be. |
|
|
|
|
|
|
|
|
When Frisky is deleted in line 40, its destructor is called. The destructor deletes each of its member pointers. If these, in turn, point to objects of other user-defined classes, their destructors are called as well. |
|
|
|
|
|
|
|
|
Every class member function has a hidden parameter: the this pointer. this points to the individual object. Therefore, in each call to GetAge() or SetAge(), the this pointer for the object is included as a hidden parameter. |
|
|
|
|
|
|
|
|
The job of the this pointer is to point to the individual object whose method has been invoked. Usually, you don't need this; you just call methods and set member variables. Occasionally, however, you'll need to access the object itself (perhaps to return a pointer to the current object). It is at that point that the this pointer becomes so helpful. |
|
|
|
|
|
|
|
|
Normally, you don't need to use the this pointer to access the member variables of an object from within methods of that object. You can explicitly call the this pointer if you want to, however; this is done in Listing 10.4 to illustrate that the this pointer exists and works. |
|
|
|
|
|
|
|
|
LISTING 10.4 USING THEthis POINTER |
|
|
|
 |
|
|
|
|
1: // Listing 10.4
2: // Using the this pointer
3:
4: #include <iostream.h>
5:
6: class Rectangle
7: {
8: public:
9: Rectangle();
10: ~Rectangle();
11: void SetLength(int length) { this->itsLength = length; }
12: int GetLength() const { return this->itsLength; }
13:
14: void SetWidth(int width) { itsWidth = width; }
15: int GetWidth() const { return itsWidth; }
16:
17: private:
18: int itsLength;
19: int itsWidth;
20: }; |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|