this pointer in C++ Programming

In C++, this pointer is used to represent the address of an object inside a member function. For example, consider an object obj calling one of its member function say method() as obj.method(). Then, this pointer will hold the address of object obj inside the member function method(). The this pointer acts as an implicit argument to all the member functions.

class ClassName {

       private:   
         int dataMember;
 
       public:
           method(int a) {

   // this pointer stores the address of object obj and access dataMember
               this->dataMember = a;
               ... .. ...
           } 
}

int main() {

    ClassName obj;
    obj.method(5);
    ... .. ...
 }

Applications of this pointer

1. Return Object

One of the important applications of using this pointer is to return the object it points. For example, the statement

return *this;

inside a member function will return the object that calls the function.

2. Method Chaining

After returning the object from a function, a very useful application would be to chain the methods for ease and a cleaner code.

For example,

positionObj->setX(15)->setY(15)->setZ(15);

Here, the methods setX, setY, setZ are chained to the object, positionObj. This is possible because each method return *this pointer.
This is equivalent to

positionObj->setX(15);
positionObj->setY(15);
positionObj->setZ(15);

3. Distinguish Data Members


Another application of this pointer is distinguishing data members from local variables of member functions if they have same name. For example,

Example 1: C++ program using this pointer to distinguish local members from parameters.

#include <iostream>
#include <conio.h>
using namespace std;

class sample
{
    int a,b;
    public:
        void input(int a,int b)
        {
            this->a=a+b;
            this->b=a-b;
        }
        void output()
        {
            cout<<"a = "<<a<<endl<<"b = "<<b;
        }
};

int main()
{
    sample x;
    x.input(5,8);
    x.output();
    getch();
    return 0;
}

A class sample is created in the program with data members a and b and member functions input() and output(). input() function receives two integer parameters a and b which are of same name as data member of class sample. To distinguish the local variable of input() data member of class, this pointer is used. When input() is called, the data of object inside it is represented as this->a and this->b while the local variable of function is represented simply as a and b.

Output

a = 13
b = -3

Example of this pointer

Example 2: C++ program to display the record of student with highest percentage.

#include<iostream>
#include<conio.h>
using namespace std;

class student
{
    char name[100];
    int age,roll;
    float percent;
    public:
        void getdata()
        {
            cout<<"Enter data"<<endl;
            cout<<"Name:";
            cin>>name;
            cout<<"Age:";
            cin>>age;
            cout<<"Roll:";
            cin>>roll;
            cout<<"Percent:";
            cin>>percent;
            cout<<endl;
        }
        student & max(student &s1,student &s2)
        {
            if(percent>s1.percent && percent>s2.percent)
                return *this;
            else if(s1.percent>percent && s1.percent>s2.percent)
                return s1;
            else if(s2.percent>percent && s2.percent>s1.percent)
                return s2;
        }
        void display()
        {
            cout<<"Name:"<<name<<endl;
            cout<<"Age:"<<age<<endl;
            cout<<"Roll:"<<roll<<endl;
            cout<<"Percent:"<<percent;            
        }
};

int main()
{
    student s,s1,s2,s3;
    s1.getdata();
    s2.getdata();
    s3.getdata();
    s=s3.max(s1,s2);
    cout<<"Student with highest percentage"<<endl;
    s.display();
    getch();
    return 0;
}

This program is used to compare the percentage of three students and display the highest among them. The concept of this pointer is used in this program. A class student is created with data members name, roll, age and percent and member functons getdata(), max() and display(). Data for each student is entered by calling getdata() function. Then, max() function is called by object s3 and s2 and s1 are passed as parameter in the function. The value of percent is compared and the object with highest percent is returned. If the object calling the method has the highest percentage then, it is returned by using this pointer as,

return *this;

Output

Enter data
Name:Paul
Age:24
Roll:11
Percent:79

Enter data
Name:Reem
Age:21
Roll:9
Percent:87

Enter data
Name:Philip
Age:23
Roll:5
Percent:81

Student with highest percentage
Name:Reem
Age:21
Roll:9
Percent:87

It should be noted that, friend function and static function cannot have this pointer. It's because friend function is not a member function of the class. And static function can be invoked without initialization of an object, i.e, static functions are not associated with any object.