1. Introduction
We know that a class can have member functions and member variables. How do we extract the calling object from inside the called member function? The answer is,this pointer. It actually refers the object that called the member function. The pointer is valid when the call is transferred to the member function. Moreover, it becomes empty when call returns.
In many circumstances, we may need to return the current object to the caller of the member function. Now, we will explore this with a suitable example.
2. The ‘this Pointer’ Example
The complete code which uses the ‘this pointer’ is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// TestIt.cpp : Defines the entry point for the // application. // #include "stdafx.h" #include "conio.h" class Point2d { public: //Sample 01: Default Ctor Point2d(int a, int b) { x = a; y = b; } //Sample 02: Function that demonstrates the usage of this Point2d* Increment() { x++; y++; return this; } //Sample 03: Function that demonstrates the usage of this Point2d Decrement() { x--; y--; return (*this); } //Sample 04: Print Co-ordinates void Print() { printf("X Co-Ordinate :%d\n", x); printf("y Co-Ordinate :%d\n", y); } private: int x; int y; }; int _tmain(int argc, _TCHAR* argv[]) { //Sample 05: Test this pointer Point2d pt1(4,4); Point2d * inc_point = pt1.Increment(); inc_point->Print(); //Sample 06: Test this pointer Point2d pt2(2,2); Point2d dec_point = pt2.Decrement(); dec_point.Print(); getch(); } |
3. Code Explanation
The constructor initializes the data members x and y from the parameters passed to the constructor.
1 2 3 4 5 6 |
//Sample 01: Default Ctor Point2d(int a, int b) { x = a; y = b; } |
Next, we write a function calledPoint2d* Increment() and the intent of this function is to increment the data members by 1 and return the current object. How do we return the current object? This is where the usage of the keyword this comes into the picture. In the below code you can see the usage of the ‘this pointer’ with the return statement.
1 2 3 4 5 6 7 |
//Sample 02: Function that demonstrates the usage of this Point2d* Increment() { x++; y++; return this; } |
The Point2d Decrement() function implementation is similar to the previous function. But this time, we return the object itself instead of returning a pointer to the current object. Below is the function which decreases the Point2d value by one.
1 2 3 4 5 6 |
Point2d Decrement() { x--; y--; return (*this); } |
The Print function just prints the values in the coordinate x, y. This function is shown below:
1 2 3 4 5 6 |
//Sample 04: Print Co-ordinates void Print() { printf("X Co-Ordinate :%d\n", x); printf("y Co-Ordinate :%d\n", y); } |
In the main, first, we created two points pt1, pt2. Then, we increment pt1 and store the return address in a pointer called inc_point. Next, we decrement the pt2 and copy the return value to the variable dec_point . The code is shown below:
1 2 3 4 5 6 7 8 9 |
//Sample 05: Client Code Point2d pt1(4,4); Point2d * inc_point = pt1.Increment(); inc_point->Print(); //Sample 06: Test this pointer Point2d pt2(2,2); Point2d dec_point = pt2.Decrement(); dec_point.Print(); |
The below picture illustrates how the objects are referred from the member functions:

This Pointer Example
The below screenshot shows the program output:

This Pointer Example: Output
Categories: C++
Tags: pointer to self, this pointer