this (computer programming)

this (computer programming)

In many object-oriented programming languages, this (also called self or Me) 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 use this. Smalltalk and others such as Object Pascal, Python, Ruby, and Objective-C use self; Visual Basic uses Me.

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 to this, 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 of this in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses of this 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 named this or self; 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 named self.

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 as this, and are called class methods.

Contents

Implementations

C++

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 now this 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 that this 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 name self is used. In class methods (created with the classmethod decorator), the first argument refers to the class object itself. In static methods (created with the staticmethod 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 to Self is to use ::.

See also

References

  1. ^ Bound member functions - Using the GNU Compiler Collection (GCC)
  2. ^ ISO/IEC 14882:2003(E): Programming Languages - C++. ISO/IEC. 2003. 
  3. ^ Stroustrup: C++ Style and Technique FAQ

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Computer programming — Programming redirects here. For other uses, see Programming (disambiguation). Software development process Activities and steps …   Wikipedia

  • computer programming language — Introduction       any of various languages for expressing a set of detailed instructions for a digital computer. Such instructions can be executed directly when they are in the computer manufacturer specific numerical form known as machine… …   Universalium

  • Computer programming in the punched card era — Punched card from a Fortran program. From the invention of computer programming languages up to the mid 1980s, many if not most computer programmers created, edited and stored their programs on punched cards. The practice was nearly universal… …   Wikipedia

  • Computer programming in the punch card era — From the invention of computer programming languages into the 1980s, many if not most computer programmers created, edited and stored their programs on punch cards. The practice was nearly universal with IBM computers in the era. In many… …   Wikipedia

  • Comment (computer programming) — For comments in Wikipedia markup, see Help:Wiki markup#Character formatting and WP:COMMENT. An illustration of Java source code with prologue comments indicated in red and inline comments in green. Program code is in blue …   Wikipedia

  • Ch (computer programming) — In computing, Ch (  /ˌsiːˈ …   Wikipedia

  • Comparison (computer programming) — Ifeq redirects here. For the MediaWiki feature, see Wikipedia:Ifeq In computer programming, comparison of two data items is effected by the comparison operators typically written as: > (greater than) < (less than) >= (greater than or… …   Wikipedia

  • Literal (computer programming) — In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating point numbers, strings, and booleans; some also have… …   Wikipedia

  • Sigil (computer programming) — In computer programming, a sigil (pronounced / sɪdʒ.ɪl/ or / sɪg.ɪl/; plural sigilia or sigils ) is a symbol attached to a variable name, showing the variable s datatype or scope. The term was first applied to Perl usage by Philip Gwyn in 1999 to …   Wikipedia

  • Null (computer programming) — Null has several meanings in computer programming.;Null pointer or null reference * Null is a special pointer value (or other kind of object reference) used to signify that a pointer intentionally does not point to (or refer to) an object. Such a …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”