+ 7

What is the use of the "this" pointer.

11th Nov 2016, 3:39 PM
User3827
5 Antworten
+ 10
This is an example explaining the use of "this" : Say you have a variable "int x " in your class and you want to declare a local variable in a method with that same name : Class MyClass { public : int x; void myFunction { x = 10; int x = 20; cout <<x <<endl; cout <<this->x <<endl; } }; This will output : 20 10
11th Nov 2016, 3:40 PM
User3827
+ 3
if i understood correctly, in C# (think it works in C++ as well) you can create a class with a variable named x and in that class have a method/function that has an argument named x and you can distinguish them with the use of the word this (this points to the variable in the class not in the method)
12th Nov 2016, 11:05 PM
Luka
+ 1
Luka "you can create a class with a variable named x and in that class have a method/function that has an argument named x and you can distinguish them with the use of the word this (this points to the variable in the class not in the method)" It's true ( to agree on your doubt ) . Simply , it points on a variable ( well, that's what 'this' in the thesaurus means ) and use that stated variable to work on value (print out ) . umm , advantage? points something outside and use it inside . Kind of like 'that' in English , but code is on paper , so we use this->this . :)
4th Feb 2017, 1:50 PM
Billy Anak Leo
Billy Anak Leo - avatar
0
yeah....it's right
3rd Feb 2017, 4:00 PM
poojitha
0
Nice explanation, thanks. I made a code to see a result as explained. https://code.sololearn.com/clhLD96GS0AP/?ref=app
2nd Mar 2018, 7:46 PM
Miroslav M
Miroslav M - avatar