What are the advantages of the 'this' keyword in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What are the advantages of the 'this' keyword in C++?

One that I've found is that it helps prevent unessesary global object instantiations which would otherwise compromise security bridges: Consider something like this; Myclass obj; class Myclass { public: Myclass (int a,int b,int c): day (a), month(b),year(c){} void date() { cout << day << " " << month << " " << year<<endl; } printInfo() { obj.date(); } private: int day, month, year; }

6th Jul 2019, 11:00 AM
Mandla Thabethe
Mandla Thabethe - avatar
1 Answer
+ 2
You don't normally need to use this. There are two situations that require it. If you declare a parameter, local variable, or local nested function with the same name as a property or method, that definition hides the class name so this.name lets you access it. If you need to access the entire object to pass as an argument, this is the only way to do so.
6th Jul 2019, 8:21 PM
John Wells
John Wells - avatar