This Pointer


Every object in C++ has access to its own address through an important pointer called thispointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.Let us try the following example to understand the concept of this pointer:

#include

using namespace std;

class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2

if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" < }
else
{
cout << "Box2 is equal to or larger than Box1" < }
return 0;
}
When the above code is compiled and executed, it produces following result:Constructor called.
Constructor called.
Box2 is equal to or larger than Box1




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.

It is possible to use the this pointer explicitly, as program below illustrates.

Using the this pointer.


      //
     // Using the this pointer

      #include

      class Rectangle
      {
      public:
           Rectangle();
           ~Rectangle();
           void SetLength(int length) { this->itsLength = length; }
           int GetLength() const { return this->itsLength; }

           void SetWidth(int width) { itsWidth = width; }
           int GetWidth() const { return itsWidth; }

      private:
           int itsLength;
           int itsWidth;
      };

      Rectangle::Rectangle()
      {
          itsWidth = 5;
          itsLength = 10;
      }
      Rectangle::~Rectangle()
      {}

      int main()
      {
      Rectangle theRect;
           cout << "theRect is " << theRect.GetLength() << " feet long.\n";
           cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
           theRect.SetLength(20);
           theRect.SetWidth(10);
           cout << "theRect is " << theRect.GetLength()<< " feet long.\n";
           cout << "theRect is " << theRect.GetWidth()<< " feet wide.\n";
      return 0;
    }

Output: theRect is 10 feet long.
theRect is 5 feet wide.
theRect is 20 feet long.
theRect is 10 feet wide.
Analysis: The SetLength() and GetLength() accessor functions explicitly use the this pointer to access the member variables of the Rectangle object. The SetWidth and GetWidth accessors do not. There is no difference in their behavior, although the syntax is easier to understand.
If that were all there was to the this pointer, there would be little point in bothering you with it. The this pointer, however, is a pointer; it stores the memory address of an object. As such, it can be a powerful tool.

You don't have to worry about creating or deleting the this pointer. The compiler takes care of that.