C++ this Pointer

C++ this Pointer: In the C++ language, this is a keyword that refers to the current instance of the class. There are three main uses for using this keyword. They are as follows:

  • It can be used to pass the current object as a parameter to another method.
  • used to refer the current class instance variable.
  • It can be used to declare indexers.

The compiler supplies an implicit pointer along with the names of the functions as ‘this’. While ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions.

C++ this Pointer

Applications of this pointer

There are three main applications used in this pointer. They are as follows:



  • Return Object
  • Method Chaining
  • Distinguish data members

Syntax:

private:
public:
method(int a) {

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

Example:

#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;
}

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

Return Object

One of the important applications of using this pointer is to return the object it points.

Example: return *this;

Method Chaining

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

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

Distinguish Data Members

They distinguishing data members from local variables of member functions if they have same name.

Example:

#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;
}

Output:

a = 13
b = -3

Note: 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.