this Pointer in C++

By: Jagan Viewed: 153269 times  Printer Friendly Format    


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
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:      };
21:
22:      Rectangle::Rectangle()
23:      {
24:          itsWidth = 5;
25:          itsLength = 10;
26:      }
27:      Rectangle::~Rectangle()
28:      {}
29:
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.



Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. this is no doubt a simple program for the learner
View Tutorial          By: shweta at 2008-07-12 01:18:59

2. Hi Shweta, Thanks for pointing the typo error in t
View Tutorial          By: Jagan at 2008-07-12 21:36:46

3. it is very useful but if you give simple programs
View Tutorial          By: krishnakumar at 2008-05-21 08:51:14

4. this pionter is used to help compiler to understan
View Tutorial          By: RUB NAWAZ at 2008-10-31 11:38:09

5. hey i can write more simple example of this pointe
View Tutorial          By: shruti at 2010-03-03 09:44:58

6. Hi Shruti, If you have better samples then feel fr
View Tutorial          By: jagan at 2010-03-03 19:42:46

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

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

9. Sir, this is very helpful for students and profess
View Tutorial          By: Pankaj Choudhary at 2010-10-19 00:24:36

10. I think this code is just simple enough to be foll
View Tutorial          By: WAQ at 2010-11-24 10:41:58

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

12. hi and thanks for this good example , i want ask i
View Tutorial          By: Maryam at 2011-12-17 11:19:07

13. The example program was very easy to understand un
View Tutorial          By: vijay at 2011-12-27 07:34:35

14. If the compiler takes care of this pointer implici
View Tutorial          By: Bhanu Noah at 2012-03-15 16:20:05

15. Hey guys i've the simple one....
class this

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