This (computer science)

This (computer science)

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, the Visual Basic language uses Me, 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. [ [http://gcc.gnu.org/onlinedocs/gcc/Bound-member-functions.html Bound member functions - Using the GNU Compiler Collection (GCC) ] ]

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)

Implementations

C++

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 now this in C++ may not be rebound. Early versions of C++ did not include references and it has been suggested that had references been included in the language from the beginning, that this would have been of reference rather than pointer type. On the contrary though, there is support for this being a pointer rather than a reference because it prevents complications that could arise from the class in question having its address-of operator overloaded.

C++ does permit objects to destroy themselves 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. Fact|date=July 2008

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.

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.

Xbase++

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

See also

* Self-reference
* Super %28computer_science%29

References

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


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • computer science — computer scientist. the science that deals with the theory and methods of processing information in digital computers, the design of computer hardware and software, and the applications of computers. [1970 75] * * * Study of computers, their… …   Universalium

  • Computer science — or computing science (abbreviated CS) is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems. Computer scientists invent algorithmic… …   Wikipedia

  • Computer science in sport — is an interdisciplinary discipline that has its goal in combining the theoretical as well as practical aspects and methods of the areas of informatics and sport science. The main emphasis of the interdisciplinarity is placed on the application… …   Wikipedia

  • Computer graphics (computer science) — This article is about the scientific discipline of computer graphics. For other uses see Computer graphics (disambiguation). A modern render of the Utah teapot, an iconic model in 3D computer graphics created by Martin Newell in 1975. Computer… …   Wikipedia

  • Computer Science (UIL) — Computer Science is one of several academic events sanctioned by the University Interscholastic League. Computer Science is designed to test students programming abilities. It is not the same as the Computer Applications contest, which tests… …   Wikipedia

  • Computer Science House — (CSH) is a special interest house founded in 1976 at the Rochester Institute of Technology, made up of a group of students who share an interest in computers, community and having fun. Despite its name, students from all majors are allowed to… …   Wikipedia

  • COMPUTER SCIENCE — The term Computer Science encompasses three different types of research areas: computability, efficiency, and methodology. General Introduction Computability deals with the question of what is mechanically computable. The most natural way to… …   Encyclopedia of Judaism

  • computer science — noun the branch of engineering science that studies (with the aid of computers) computable processes and structures • Syn: ↑computing • Topics: ↑computer, ↑computing machine, ↑computing device, ↑data processor, ↑electronic computer, ↑ …   Useful english dictionary

  • Computer Science Tripos — The Computer Science Tripos is the undergraduate course in computer science offered by the University of Cambridge Computer Laboratory. It evolved out of the Diploma in Computer Science, the world’s first taught course in computer science, which… …   Wikipedia

  • 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… …   Wikipedia

Share the article and excerpts

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