3.3 Anonymous Objects




In some cases an object is needed only temporarily. Explicitly introducing a variable to name this temporary object can be avoided by creating an anonymous object. An anonymous object is an object in every sense except that it has no name. Consider the following example:

   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);


Assume that all of the Location and Shape objects have no other uses than the ones shown in the example.

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 more efficient use of memory to be able to delete the Location object as soon as it 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 used again later.

Anonymous objects are a more efficient and clearer way to create and use an object that is intended only to be used in a single (localized) place. These objects are called "anonymous" objects because they are not given a name. Instead the object is declared 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) );


In the construction of the window object two anonymous objects are created, one anonymous Location object and one anonymous Shape object. The display object is also created using two anonymous objects. Finally, the MoveTo and Resize operations also use anonymous objects. It is important to realize that the example with named objects and the one with anonymous objects have exactly the same observable effects.

Anonymous objects are also useful in giving 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 than the default Location of (10,10) will be used.

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 it needed and destructed as soon as its immediate use is over. This reduces to a minimum the lifetime of the anonymous object (equivalently, reducing 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.



Next Stop


Exercises

None as yet.

Last updated: July 3, 1996 / kafura@cs.vt.edu