< Prev
Next >

C++ this Keyword

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


using namespace std;

class A
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;
cout<< "Value in a : " << ob.a;


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 -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< Pointer to member functions
Function Call by Reference >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next