Advertisement



< Prev
Next >



C++ this Keyword










Using this keyword, we can access the currently executing object and its data members.

#include<iostream>

using namespace std;

class A
{
public: 
int a;

void putValue(int);
};


//Defintion of putValue() function of A class 
void A :: putValue(int a)
{

//accessing an data member with this keyword, to differ it from a local variable with same name
this->a = a; 
}


int main()
{
A ob;
ob.putValue(10);
cout<< "Value in a : " << ob.a;
}


Output-


Value in a : 10

Here in the putValue() function, we have accessed an data member a with this keyword, in order to distinguish its name from a local variable a, which resolves the shadowing issue.



Please share this article -





< Prev
Next >
< Pointer to member functions
Function Call by Reference >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement