Topic : Smart pointer templates in C++
Author : David Harvey
Page : << Previous 4  
Go to page :


void Agent::Enter(ObjVar<Location> NewLoc)
        CurrentLoc->AgentLeaves(this);  // NB!
        CurrentLoc = NewLoc;
        CurrentLoc->AgentEnters(this);  // NB!

However, the pointer to the current agent this is a C++ pointer-to-agent, not an object variable. The compiler will convert this pointer into a temporary object variable in each of the calls to the Location member functions: this ObjVar will believe it is the first reference to the wrapped agent, and when it goes out of scope at the end of the function, the wrapped agent pointer will be deleted! We can prevent the compiler from making the conversion automatically through using the new explicit keyword in the declaration of the objvar template constructor:
template <class T>
class ObjVar
        explicit ObjVar(T* pT);

This at least means that accidental conversions and consequent deletions of wrapped objects cannot occur. But it does leave a hole in the idiom, and forces us to make a third object responsible for such interactions: in the current case, this third object must of course hold the identities of both Agent and Location as ObjVars. Surprisingly, there is a way around this problem, which will have to wait for a subsequent article as space is already short here.
A second issue also arises from objects which hold mutual references. If these ever become the only references held by these objects, then we end up with objects which can never be reclaimed by the ObjVar mechanism - in effect, a good old-fashioned memory leak. The situation extends to cycles of three or more links. To avoid this, we must take care that at least one object is responsible for breaking the cycle.

In spite of this, these typed object variables demonstrate the power of templates beyond managing collections of objects. Reference-counted smart-pointers offer the flexibility of pointers in implementing associations but, by taking on the burden of reference counting and management of sharing, largely free us from worrying about the deletion of individual object instances. Although the extra indirections make function call marginally slower, the only penalty in code size arises from specialising the template class member functions: in particular, the fact that ObjVars contain only a pointer makes them efficient to pass into and return from functions. By using object variables to wrap specialised instances of abstract base classes, we can benefit both from polymorphism and from a measure of automatic memory management.

ANSI[95]: ISO Working Paper for Draft Proposed International Standard for Information Systems-Programming Language C++, Doc No: X3J16/95-0087 WG21/N0687, April 1995
Myers[95]: N. Myers, 'A new and useful template technique: "Traits"', C++ Report, May 1995, 33-35.

Stepanov[95]: A. Stepanov/M. Lee, The Standard Template Library ISO Programming Language C++ Project, Doc No: X3J16/94-0095, WG21/N0482, May 1994. This material is now incorporated in ANSI[95].

Stroustrup[91]: B.Stroustrup, The C++ Programming Language, 2nd Edition, Addison Wesley, 1991.

Stroustrup[94]: B. Stroustrup, 'Making a vector fit for a standard', C++ Report, October 1994, 30-34.

Veldhuizen[95a]: T. Veldhuizen, 'Using C++ template metaprograms', C++ Report, May 1995, 36-43.

Veldhuizen[95b]: T. Veldhuizen, 'Expression Templates', C++ Report, June 1995, 27-31.

Vilot[94]: M. J. Vilot, 'An Introduction to the Standard Template Library', C++ Report, October 1994, 22-29.

Page : << Previous 4