What Is A Well-Behaved Object?

 < Day Day Up > 



A well-behaved object is one that can be relied upon to perform as if it were a native C++ data type. The degree to which an object should perform like a native data type is context dependent. For instance, a Person object may not be used in all the same situations as an int, float, or double, but in the contexts in which a Person object participates it should perform flawlessly.

Object Usage Contexts

When creating user-defined types you must be aware of the object usage contexts of object creation, object copying, object assignment, and object destruction.

Object Creation

The first object usage context you must be aware of is object creation. Objects come into existence in several ways. The first and perhaps most common form of object creation is by simply declaring a static variable as the following line of code illustrates:

Foo f;

Here an object named f of type Foo is declared. If Foo is a user-defined type with complex data type member attributes then those complex attribute objects must also be created properly when the Foo object comes into existence.

Another method of creating objects is to use the new operator and create the object dynamically in heap memory. The following line of code gives an example:

Foo *foo_ptr = new Foo;

In this example a Foo object is created dynamically and its address is assigned to the foo_ptr pointer. The same issues apply regarding the proper creation of any complex data members Foo might contain.

A third, and often unconsidered, way objects are created is when they are passed to functions by value. Examine the following code snippet:

void functionA(Foo f);      //function declaration Foo my_foo;                 //declare Foo object functionA(my_foo);          //call functionA() with Foo object 

In this example, when functionA() is called with the my_foo object, the function parameter f is created behind the scenes by the compiler at the time of the function call.

A fourth way objects are created is when functions return object values.

Quick Review

Objects come into existence in several ways: via static object declaration, via dynamic object creation with the new operator, when objects are passed to functions by value, and when an object is returned from a function.

Object Copying

The second object usage context is object copying. Object copying and object creation are related in that objects can be created and initialized using an existing object as a guide. Objects are created by copy when an object is passed to a function, as was discussed in the previous section, and when object values are returned from functions, also discussed above.

In the object copy context usage, an object can play one of two roles. The first is the role of the object being created; the second is the role of the object being copied.

Object Assignment

The third object usage context is object assignment. In object assignment, an existing object is being assigned the state values of another existing object. Objects participating in the object assignment context can play two roles: that of the object whose state values are being changed, and that of the object whose state values are being copied.

Object assignment is different from object copying in that object assignment deals with two existing objects while object copy deals with one existing and one new object.

With object assignment, you must be aware of the difference between a shallow copy and a deep copy. Also, the object whose state values are being changed must properly manage its currently held resources before taking on its new state.

Object Destruction

The fourth object usage context you need to be aware of is object destruction. Objects exist for a specific lifetime based on their location within a program. When an object’s lifetime expires its destructor is called. The destructor’s job is to ensure the release of any resources the object may have used during its lifetime. For example, if the object allocated any dynamic memory during its lifetime, it should release the dynamic memory upon its destruction.

Other Contexts By Design

The object usage contexts presented above represent the four basic contexts objects will participate in at a minimum. You alone, as a programmer, know the other contexts in which your user-defined objects will participate. An example might be the use of Person class objects in a sorting algorithm, where one Person object is compared in some way with other Person objects. If Person objects are to behave correctly in these comparison contexts then the Person class must be developed in a manner that supports such context participation.

The usage contexts presented above are summarized in table 17-1.

Table 17-1: Object Usage Contexts

Object Usage Context

Be Aware of...

object creation

Objects are created via static declaration, via dynamic allocation using the new operator, and via copy when object values are passed to and returned from functions. When objects are created they must properly initialize their member attributes.

object copying

Objects participate in the copy context when a new object is created using an existing object as a guide. The copy context occurs primarily when objects are passed to and returned from functions. The object being created will use the existing object’s attributes to initialize its own member attributes.

object assignment

Objects participate in the object creation context when an existing object’s state is set to that of another existing object. In the assignment context, the object that’s being changed must properly handle its complex data members. Thus it is necessary to understand the difference between a shallow copy and a deep copy.

object destruction

Objects participate in the object destruction context when they reach the end of their lifetime in a program. The object being destroyed must properly release any allocated resources before destruction completes.

other contexts by design

User-defined data types you create are used in addition to the four basic usage contexts.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net