Object computer science

An object is a unique concrete instance of an abstract data type, a class (that is, a conceptual structure including both data and the methods to access it) whose identity is separate from that of other objects, although it can "communicate" with them via messages.

In some occasions, some object can be conceived of as a sub program which can communicate with others by receiving or giving instructions based on its, or the other's, data or methods. Data can consist of numbers, literal strings, variables, references. An object is often thought of as a region of storage.

One of the benefits claimed for object-oriented program is that it can model real life problems "naturally" by a sort of simulation. This type of system is usually constructed in a class-based language, where an instance is an "instantiated" object of a particular class. Each instance is a variation on a general theme, one which is defined by the parent class. For example, my dog "Killer" could be described as an instance of a dog (where dogs are a class of animal).

A kind of objects:

  • Function object - one puts a function to an object and pass it around instead of the function. Such an object is called "Function object".
  • Immutable object - an object whose state is fixed in the creation and does not vary afterward.
  • First-class object

Duplication

A duplication of an object is commonly done by copying each attribute and objects belonging to the object recursively. This is usually time-consuming. The duplication of lists, for instance, involves copying each element.

Defensive copy is an act to ensure that the object retains a state that is at the moment of duplication. An interesting problem has been addressed [1] (http://www.sgi.com/tech/stl/string_discussion.html), which is related to the implementation of string class in STL:

# include <string>
# include <stdio.h>

main() {
   string s("abc");
   string t;
   char & c(s[1]);

   t = s;       // Data typically shared between s and t.
   c = 'z';     // How many strings does this modify?
   if (t[1] == 'z') {
        printf("wrong\n");
   } else {
        printf("right\n");
   }
}

The specification of C++ understandably says t should not be changed. This problem is only mitigated by the use of defensive copy, which in turn wipes out any benefit gained by the use of reference counting.

See also


de:Objekt_(objektorientierte_Programmierung) ja:インスタンス

This article is licensed under the GNU Free Documentation License. It uses material from Wikipedia article. Browse Wikipedia for more information.