this Pointer in C++

By: Jagan Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

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
2. View Comment

this is no doubt a simple program for the learner to begin with,yet there are some printing mistakes specially in the output shown.this mistake may misguide the new learner.kindly update this for us.

View Tutorial          By: shweta at 2008-07-12 01:18:59
3. View Comment

Hi Shweta, Thanks for pointing the typo error in the output. Instead of <b>wide</b>, the word <b>long</b> was used. It has been corrected now. So the Output is correct now.

View Tutorial          By: Jagan at 2008-07-12 21:36:46
4. View Comment

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
5. View Comment

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
6. View Comment

Hi Shruti, If you have better samples then feel free to share it here. Your sample will also be published for everybody else to learn. :)

View Tutorial          By: jagan at 2010-03-03 19:42:46
7. View Comment

this helps a lot

View Tutorial          By: Ashar at 2010-04-28 04:47:36
8. View Comment

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. View Comment

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

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

I think this code is just simple enough to be followed yet complex enough to show how the 'this' pointer can be used practically.
Otherwise simple example tend to be so simple that the learner has difficulty going about a more complex problem.
Hats off to Jagan - Thanks!

I think I finally get it now.

View Tutorial          By: WAQ at 2010-11-24 10:41:58
11. View Comment

Very Easy To Understand

View Tutorial          By: Najeeb at 2011-06-09 04:12:34
12. View Comment

hi and thanks for this good example , i want ask is there any another example of using "this poiner" , in anohter uses ? if yes i need this examples . thank you
very fast i need help . thanks a lot

View Tutorial          By: Maryam at 2011-12-17 11:19:07
13. View Comment

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
14. View Comment

If the compiler takes care of this pointer implicitly calls then what is the use of this pointer..please answer me

View Tutorial          By: Bhanu Noah at 2012-03-15 16:20:05

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Jagan
Importing a Certificate for the CA example using keytool in Java
The if-then-else Statement in Java
WHERE Clauses in SQL
The TryCatchFinally Interface in JSP
assert() Function Example program in C
Guidelines for Struts Application Development
Using printf function in C
File Inclusion in C
Using Multiple Message Resource Bundles in Struts
While and For Loops in C
Arrays sample program in C
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using cout.fill() in C++
Types of Profiles in J2ME
DateField sample program in J2ME

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
The Stack and the Free Store in C++
Getting User Input Using cin in C++
Function overloading in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
do...while Loops in C++
for Loops in C++
Using switch Statements in C++
What Is a Pointer in C++?
The indirection operator (*) - dereference operator.
Dot (.) vs Arrow (->) to access data members in C++
this Pointer in C++
Converting Pointer Parameters in C++ to Java
Converting C++ Abstract Classes into Java Interfaces
Converting C++ Multiple-Inheritance Hierarchies to Java
Most Emailed Articles (in C++)
Demonstrating global and local variables in C++
C++ and Object-Oriented Programming
Using cout in C++
Use of Conditional (Ternary) Operator in C++
const Member Functions in C++
Classes with Other Classes as Member Data in C++
What Is a Pointer in C++?
Manipulating Data by Using Pointers
Stray or Dangling Pointers in C++
C++ Destructors Versus Java Finalization
atoi(), itoa() in C++
strcat() and strncat() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Getting User Input Using cin in C++
Pass by Reference in C++ functions