+ 7
What is the use of the "this" pointer.
5 Answers
+ 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
+ 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)
+ 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 .
:)
0
yeah....it's right
0
Nice explanation, thanks.
I made a code to see a result as explained.
https://code.sololearn.com/clhLD96GS0AP/?ref=app