+ 1
can someone please explain the use of "this" i've been seeing it a lot in getters and setters but its not fully clicking with me
2 Antworten
+ 4
Specify the language you're asking.
Generally, it's a pointer or reference to the current object. For example consider a class Test and a method print(). When you call obj.print(), the compiler automatically passes an extra argument with the function call which is called "this" pointer or reference(based on the language, pointer in C++). When calling the function, you know the object you're working with, but how do the function know which object to act on? To solve this, the compiler passes "this" to specify the object to act on. "this" is just a name chosen by the developer community for convenience. Feel free to ask if you got any more doubts on "this"
Below is an example for demonstrating "this"
https://code.sololearn.com/cdGyM55jE9g7/?ref=app
0
Please always tag the language you're asking about.
https://code.sololearn.com/W3uiji9X28C1/?ref=app