- this (computer programming)
-
In many object-oriented programming languages,
this
(also calledself
orMe
) is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it (such as Java, C#, and PHP) generally usethis
. Smalltalk and others such as Object Pascal, Python, Ruby, and Objective-C useself
; Visual Basic usesMe
.The concept is the same in all languages. For brevity, here we just say
this
.this
is usually an immutable reference or pointer which refers to the current object. Some languages, such as Objective-C, allow assignment tothis
, although it is deprecated[citation needed]. Doing so can be very misleading to maintenance programmers, because the assignment does not modify the original object, only changing which object that the rest of the code in the method refers to, and can end with undefined behavior[citation needed].After an object is properly constructed
this
is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to bring symbols within their class visible. In the latter case, the use ofthis
in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses ofthis
in this case, such as referring to instance variables hidden by local variables of the same name, or if the method wants to return a reference to the current object, i.e.this
, itself.this
becomes an extra parameter to an instance method. For example, the following instance method in C++int foo::print (bar x)
is essentially equivalent to the procedural programming:
int foo_print (foo *const this, bar x)
In some languages, for example Python and Perl 5,
this
is made explicit, the first parameter of an instance method being such a reference. It needs explicitly to be specified. In this case, the parameter need not necessarily be namedthis
orself
; like any other parameter, it can be freely named by the programmer; however, by informal convention, the first parameter of an instance method in Perl and Python is namedself
.In some compilers (for example GCC), pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit
this
pointer parameter.[1]Static methods in C++ or Java are not associated with instances but classes, and so cannot use
this
, because there is no object. In others, such as Python, Ruby, Smalltalk or Objective-C, the method is associated with a class object that is passed asthis
, and are called class methods.Contents
Implementations
C++
For more details on this topic, see C++ classes.Early versions of C++ would let the
this
pointer be changed; by doing so a programmer could change which object a method was working on. This feature was eventually removed, and nowthis
in C++ is an rvalue[2] .Early versions of C++ did not include references and it has been suggested that had they been so in C++ from the beginning,
this
would have been a reference, not a pointer.[3] But on the other hand, others[who?] say thatthis
being a pointer makes life simpler because it avoids complications that could arise from the class having its address-of operator overloaded.C++ lets objects destroy themselves with the idiom
delete this
.Java
A Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods.
Since all instance methods are virtual in Java,
this
can never be null."...the reason for using this construct [this] is that we have a situation that is known as name overloading - the same name being used for two different entities....It is important to understand that the fields and the parameters are separate variables that exist independently of each other, even though they share similar names. A parameter and a field sharing a name is not really a problem in Java." - Objects First with Java, D. Barnes and M. Kölling
C#
this
in C# works the same way as in Java, for reference types. However, within C# "value types",this
has quite different semantics, being similar to an ordinary mutable variable reference, and can even occur on the left side of an assignment.Dylan
In Dylan, an OO language that supports multimethods and hasn't a concept of
this
, sending a message to an object is still kept in the syntax. The two forms below work the same; the differences are just syntactic sugar.object.method(param1, param2)
and
method (object, param1, param2)
Python
In Python, there is no keyword for
this
, but it exists as the name of the obligatory first argument of all member functions, to which the object instance it is called on is automatically bound. Conventionally, the nameself
is used. In class methods (created with theclassmethod
decorator), the first argument refers to the class object itself. In static methods (created with thestaticmethod
decorator), no special first argument exists.Self
The Self language is named after this use of "self".
Xbase++
Self
is strictly used within methods of a class. Another way to refer toSelf
is to use::
.See also
- Self-reference
- Super (computer science)
References
- ^ Bound member functions - Using the GNU Compiler Collection (GCC)
- ^ ISO/IEC 14882:2003(E): Programming Languages - C++. ISO/IEC. 2003.
- ^ Stroustrup: C++ Style and Technique FAQ
External links
- The Design and Evolution of C++ by Bjarne Stroustrup - Addison-Wesley Pub Co; 1st edition (March 29, 1994); ISBN 0-201-54330-3
- More Effective C++: 35 New Ways to Improve Your Programs and Designs by Scott Meyers -- (1995) ISBN 0-201-63371-X
- Java this
Categories:
Wikimedia Foundation. 2010.