Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

this Pointer in C++

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();
}

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor