|
|
Object (Computer Science)An object is the central concept in the object-oriented programming programming paradigm. Objects consist of run-time structures containing two kinds of members: - data members
- methods that access the data members in predefined ways.
In the case of most objects, one can access the data members only through the method members, making it easy to guarantee that the data will always remain in a well-defined state (class invariants will be enforced). A real-world example of an object would be "my dog", which is an instance of a type (a class) called "dog", which is a subclass of a class "animal". In the case of a polymorphic object, some details of its type can be selectively ignored, for example a "dog" object could be used by a function looking for an "animal". So could a "cat", because it too belongs to the class of "animal". While being accessed as an "animal", some member attributes of a "dog" or "cat" would remain unavailable, such as the "tail" attribute, because not all animals have tails. Three properties characterize objects: - identity - the property that the object can be distinguished from other objects
- state - describes the data stored in the object
- behaviour - describes the methods in the object's interface by which the object can be used
Some terms for specialized kinds of objects include: - Singleton object - a one-of-a-kind object, i.e. no other objects of its class are ever created during the lifetime of the program.
- Functor function object - an object with a single method (in C++, this method would be the function operator, "operator()") that acts much like a function (like a C/C++ pointer to a function).
- Immutable object - an object set up with a fixed state at creation time and which does not vary afterward.
- First-class object
- Container object
- Factory object
Duplication A built-in method called a copy constructor can be used to easily duplicate most objects. Programmers typically implement the copy constructor as a recursive copy of all attributes and of all objects belonging to the object, one-by-one. This can take a lot of time. The duplication of lists, for instance, involves copying each element. Implementors may set up some copy constructors with reference counting so that identical copies of an object do not take up additional memory. This technique interacts badly with low-level access to internal object data. Defensive copy is an act to ensure that the object retains a state that exists at the moment of duplication. Code here addresses an interesting problem which relates to the implementation of string class in the STL: - include
- include
using namespace std; int main() { string s("abc"); string t; char & c(s1); t = s; // Data typically shared between s and t. c = 'z'; // How many strings does this modify? if (t1 == 'z') cout << "wrong" << endl; else cout << "right" << endl; } The specification of C++ understandably frowns on changing t. One can only mitigate this problem by the use of defensive copy, but this in turn wipes out any benefit gained by the use of reference counting. See also
|
 |