this Pointer in C++
By: Jagan in C++ Tutorials on 2007-09-14
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.
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments