![]() |
|||||||||
3.3 Anonymous Objects |
|||||||||
|
Location initialLocation(100, 100),
displayLocation(200,200);
Shape initialShape(150, 200),
displayShape(300, 200);
Frame window (initialLocation, initialShape);
Frame display (displayLocation, displayShape);
...
Location newLocation(300,300);
Location newShape (150,150);
window.MoveTo(newLocation);
display.Resize(newShape);
Using explicitly declared and named objects that are used only once has several disadvantages. First, the programmer has to write a separate declaration and invent a meaningful name for an object that plays a very minor, perhaps trivial role. This effort may not be seen as worthwhile. Second, the object may continue to exist for some time after it has been used even though it is not needed. In the above example, the scope of "initialLocation" and "window" are the same. However, the window object may be needed far longer than the Location object, though both objects will continue to exist. It is a more efficient use of memory to be able to delete the Location object as soon as its useful lifetime has ended. Third, the reader of the code is not given a clear indication that objects such as "initialLocation" and "displayLocation" are, in fact, used in such a localized way. Only by reading all of the code in this scope can it be determined whether those objects are used only once or again later. Anonymous objects are a more efficient and clearer way to create and use an object that is needed only to be used in a single, localized place. An anonymous object is constructed directly in the place where it is needed. The example above can be re-written using anonymous objects as
follows: Frame window ( Location(100,100), Shape(150, 200) ); Frame display ( Location(200,200), Shape(300, 200) ); ... window.MoveTo( Location(300,300) ); display.Resize( Shape(150,150) );
Anonymous objects are also useful in providing the default value for a parameter that is an object of a class rather than a built-in type. For example, the MoveTo method in the Frame class takes a Location object as its parameter. If a default value was desired for this parameter, it could be specified as follows:
class Frame {
private:
...
public:
...
void MoveTo (Location loc = Location(10,10));
...
};
This definition of the MoveTo method specifies that if the MoveTo method is invoked with no arguments, then the default Location of (10,10) will be used. Moreover, anonymous objects are used in assigning a new value
or updating the value of an object. Suppose that a Frame object
named "window", has been created using a Location object, named
"current." The Frame is to be shifted by a relative amount (five
pixels in each direction) on the screen. What is needed is a way
to update the Location object. However, the Location class does
not define any mutator methods that allow Location objects to
be updated - they can only be constructed and examined. Three
different ways of using anonymous objects are shown in the following
code examples. // First example Location current(100,100); Frame window (current); ... window.MoveTo( Location(current.XCoord()+5, current.YCoord()+5) );
// Second example Location current(100,100); Frame window (current); ... current = Location(105,105); window.MoveTo( current );
// Third example Location current(100,100); Frame window (current); ... current = Location(current.XCoord()+5, current.YCoord()+5); window.MoveTo( current );
Notice that anonymous objects remove the three problems noted above. First, no separate declaration is needed for the anonymous objects. They are simply created at the site of use without the need for an identifier name. Second, the object is constructed only when its needed and destructed as soon as its immediate use is over. This reduces to a minimum the lifetime of the anonymous object and reduces its resource usage. Third, it is clear to the reader that the anonymous object is not used elsewhere - such other usage being impossible since there is no way to refer to the object.
Tasks
|
|||||||||
|
|||||||||