Home >>C++ Tutorial >C++ This Pointer

C++ This Pointer

This Pointer in C++

The current instance of the class is referred to by this keyword in C++ programming hence; this is known as the this Pointer in C++.

In terms of usage, there are these three main uses of this keyword in the C++ programming language that are mentioned below:

  • This keyword in C++ language is generally used to refer current class instance variable.
  • This keyword in C++ language is generally used to declare indexers.
  • This keyword in C++ language is generally used to pass the current object as the parameter to another method.

C++ this Pointer Example

Here is an example of the this keyword in the C++ programming language referring to the fields of the current class:

#include <iostream>  
using namespace std;  
class Student 
	{  
		public:  
       int roll_no;     
       string name;  
       float marks;  
       Student(int x, string y, float z)    
        {    
            this->roll_no = x;    
            this->name = y;    
            this->marks = z;   
        }    
       void show()    
        {    
         cout<<"Student Name "<<roll_no<<endl;
		 cout<<"Student Roll "<<name<<endl;
		 cout<<"Student Marks "<<marks<<endl;    
        }    
};  
int main(void) {  
    Student stu =Student(102, "Shishir",90);  
    stu.show();    
    return 0;  
}  
Output :
Student Name 102
Student Roll Shishir
Student Marks 90

No Sidebar ads