this (computer science)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked. C++, and programming languages which derive from it (such as Java and C#) generally use the keyword this, whereas Smalltalk and its progeny, as well as Python prefer the keyword self. The Self programming language is named after this use of "self".

In all of the above languages, this/self is an immutable reference (a pointer in C++) which refers to the current object. It is generally guaranteed to be a valid reference (it is never the null pointer). Some languages require its use to refer to the current object; other languages use lexical scoping to make symbols defined within the corresponding class definition visible, making use of this unnecessary in some instances.

Behind the scenes, this/self becomes an extra implicit argument to the instance method. For example, the following instance method in C++

int foo::print (bar x)

is essentially equivalent to the following function in non-object-oriented programming:

int foo_print (foo *const this, bar x)

In some languages without special object-oriented syntax, for example Perl 5, this is made explicit, in that the first argument of an instance method is a reference to the instance, and needs to be handled explicitly. In some compilers (e.g. GCC), pointers to C++ instance methods can be directly cast into a pointer to the appropriate non-object-oriented function with an explicit this pointer argument.[1]

Static methods in C++ or Java are not associated with an object, and therefore cannot use a this/self keyword. In other languages, such as Python, Ruby, or Smalltalk, they are associated with a class object that is passed as this/self, and are more often called class methods.

In Dylan, an OO language which supports multimethods and thus doesn't have a concept of this, the notion of sending a message to an object is nonetheless retained in the syntax. The following two forms are equivalent; the former is deemed to be syntactic sugar for the latter:

object.method(argument1, argument2)

and

method (object, argument1, argument2)

Contents

For more details on this topic, see C++ structure.

Early dialects of C++ were unusual in that they permitted the this pointer to be modified; in this way a method could "switch" which object it was working on. This feature was eventually deprecated, and this in C++ may not be rebound. (Early versions of C++ did not include references; had references been included in the language from the beginning, it is likely that this would have been of reference rather than pointer type -- the semantics of this in C++ resemble references far more than pointers).

C++ does permit objects to "commit suicide" with the construct delete this; this operation, when performed, invalidates the object (and the this pointer). Some C++ authors now consider delete this to be a harmful construct and advise against its use.

Some implementations of C++ allow a non-virtual instance member function to be called on a NULL pointer. Some examples can be found in MFC:

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

The ISO C++ Standard states, however, that the behaviour of such code is undefined.

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

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.

Self is strictly used within methods of a class. Another way to refer to Self is to use ::

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.