When a member function is called using an object, the address of the object is implicitly passed to the member function as the first argument (by the compiler) The member function collections this address in a pointer named this.

Simple Example of this Pointer In C++ program

#include<iostream>
using namespace std;
class This_Pointer{ 
int a;
public: void setData(int a) { 
this->a = a; } 
void printData(){ 
cout<<"The value of a is"<<a<<endl;
} }; 
int main(){ 
This_Pointer tp;
tp.setData(10); 
tp.printData();
return 0;
 }

Output:-The value of a is 10

Explanation of the program

In this program, the ‘a’ is a private member of the class This_Pointer. Also, the arguments received by the member function printData() is also a. Hence, if we do not use this pointer explicitly both the ‘a’ will be considered as data members.

This article is written by our awesome writer
Comments to: This pointer In C++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password