Jump to content

this (computer programming)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Jaberwocky6669 (talk | contribs) at 06:28, 17 December 2009 (disambiguated wikilink: keyword --> keyword|Keyword (computer porogramming)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 programming 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. Doing so can be very misleading to maintenance programmers, because the assignment does not modify the original object, only where it lives, 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.

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 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, or Smalltalk, the method is associated with a class object that is passed as this, and are called class methods.

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 deprecated, and now this in C++ is const[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[citation needed]. But on the other hand, others 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. This mostly comes down to troubles with the type system, something that in C++ is well-defined but rather complex.

C++ lets objects destroy themselves with the idiom delete this. If done it invalidates the object and the this pointer.

Some implementations of C++ allow a non-virtual instance member function to be called on an invalid pointer, in particular the NULL pointer. Examples can be found in MFC:

HWND CWnd::GetSafeHwnd()
{
    return ( (this==NULL) ? NULL : this->m_hwnd);
}

The ISO C++ Standard states, however, that such code has undefined behavior.

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. Conventionally, the name self is used.

Self

The Self programming 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.
  • 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