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

Launch Your Dating App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Dating App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Grocery Delivery App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Grocery Delivery App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Courses App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Courses App with Our Step-by-Step Course and Ready-Made Template.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com