Using the this pointer : this « Class « C++ Tutorial






#include <iostream>
 
 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;
     std::cout << "theRect is " << theRect.GetLength() 
               << " feet long.\n";
     std::cout << "theRect is " << theRect.GetWidth() 
               << " feet wide.\n";
 
     theRect.SetLength(20);
     theRect.SetWidth(10);
     std::cout << "theRect is " << theRect.GetLength()
               << " feet long.\n";
     std::cout << "theRect is " << theRect.GetWidth()
               << " feet wide.\n";
 
     return 0;
 }
theRect is 10 feet long.
theRect is 5 feet wide.
theRect is 20 feet long.
theRect is 10 feet wide.








9.27.this
9.27.1.Use the 'this' pointer.
9.27.2.Using the this pointer
9.27.3.Returning the dereferenced this pointer
9.27.4.Cascading member function calls with the this pointer