this Pointer in C++

Article by: Manish Methani

Last Updated: October 17, 2021 at 8:04am IST
3 min 58 sec read

this pointer

If we want an address of an object within same class then this pointer is used. It is used as self reference within a class.

#include 
class Student 
{
int rollNo;
char name[20];
public:
    Student (int rollNo, char name[])
    {
     this -> rollNo = rollNo;
     strcpy(this -> name , name);
    }

     void showInfo()
     {
       cout<<"Roll No is " <// Function returning address of object 
     Student *getAddress()
     {
       return this;
     }

Function returning object     
Student getObject()
{
return *this;
}
}; 

void main()
{
Student a(64,"Manish");
Student *p = a.getAddress();
Student b = a.getObject();
}

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com