this Pointer in C++

By: Jagan  

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.

1:      // 
2:      // Using the this pointer
4:      #include <iostream.h>
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; }
14:           void SetWidth(int width) { itsWidth = width; }
15:           int GetWidth() const { return itsWidth; }
17:      private:
18:           int itsLength;
19:           int itsWidth;
20:      };
22:      Rectangle::Rectangle()
23:      {
24:          itsWidth = 5;
25:          itsLength = 10;
26:      }
27:      Rectangle::~Rectangle()
28:      {}
30:      int main()
31:      {
32:           Rectangle theRect;
33:           cout << "theRect is " << theRect.GetLength() << " feet long.\n";
34:           cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
35:           theRect.SetLength(20);
36:           theRect.SetWidth(10);
37:           cout << "theRect is " << theRect.GetLength()<< " feet long.\n";
38:           cout << "theRect is " << theRect.GetWidth()<< " feet wide.\n";
39:      return 0;
40: }

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.

Archived Comments

1. Hey guys i've the simple one....
class thiskeyword
int x,y;
void getda

View Tutorial          By: Ajay Khowal at 2012-07-13 16:25:42

2. If the compiler takes care of this pointer implicitly calls then what is the use of this
View Tutorial          By: Bhanu Noah at 2012-03-15 16:20:05

3. The example program was very easy to understand unknown people so give to example for small programs
View Tutorial          By: vijay at 2011-12-27 07:34:35

4. hi and thanks for this good example , i want ask is there any another example of using "this po
View Tutorial          By: Maryam at 2011-12-17 11:19:07

5. Very Easy To Understand
View Tutorial          By: Najeeb at 2011-06-09 04:12:34

6. I think this code is just simple enough to be followed yet complex enough to show how the 'this' poi
View Tutorial          By: WAQ at 2010-11-24 10:41:58

7. Sir, this is very helpful for students and professionals those are interested in pointer .

View Tutorial          By: Pankaj Choudhary at 2010-10-19 00:24:36

8. i can write much easy program than this its more complicated
View Tutorial          By: nand kishor singh at 2010-08-10 12:24:22

9. this helps a lot
View Tutorial          By: Ashar at 2010-04-28 04:47:36

10. Hi Shruti, If you have better samples then feel free to share it here. Your sample will also be publ
View Tutorial          By: jagan at 2010-03-03 19:42:46

11. hey i can write more simple example of this pointer.........why such confusing examples????
View Tutorial          By: shruti at 2010-03-03 09:44:58

12. this pionter is used to help compiler to understand which datatype is property and which is method..
View Tutorial          By: RUB NAWAZ at 2008-10-31 11:38:09

13. Hi Shweta, Thanks for pointing the typo error in the output. Instead of <b>wide</b>, the
View Tutorial          By: Jagan at 2008-07-12 21:36:46

14. this is no doubt a simple program for the learner to begin with,yet there are some printing mistakes
View Tutorial          By: shweta at 2008-07-12 01:18:59

15. it is very useful but if you give simple programs the viewer can understand easily
View Tutorial          By: krishnakumar at 2008-05-21 08:51:14

Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial