AutoPtrRef< OtherType > Struct Template Reference


Detailed Description

template<typename OtherType>
struct MaxSDK::AutoPtrRef< OtherType >

Helper class used to implement destructive copy semantics and allow for AutoPtrs to be used as return values.

You don't really need to know this since the compiler will use AutoPtrRefs silently and everything will just work out the way you'd expect, but here's an explanation of why AutoPtrRef is needed.

Values returned by functions are temporaries (unnamed objects that you never actually see in your source code). These then get copied into whatever variable is set up to receive them. (See More Effective C++, items 19 and 20.)

Since these temporaries are inaccessible to normal source code, it is usually a design error for them to be modified, so the compiler makes them const. They can't be bound to non-const references, which are needed for the AutoPtr copy constructor and assignment operator. Attempting to return and receive an AutoPtr from a function would then generate a compiler error.

The AutoPtrRef and cast operations below exist to allow the compiler to implicitly convert from the const AutoPtr returned from a function to an AutoPtrRef, which can then be assigned to the receiving AutoPtr using the AutoPtr(AutoPtrRef&) constructor.

(Okay, there's a bit more going on to get around the double-conversion rule, which Herb Sutter explains as: "[A] subtle interaction in the overload resolution rules allows the selection of single viable constructor function, followed by the manufacture of just the right temporary which lets us slip in a second user-defined conversion (to auto_ptr_ref<Z>) that is normally forbidden by 12.3/4." From the thread "What is auto_ptr_ref for?" on comp.lang.c++.moderated.)

#include <autoptr.h>

List of all members.

Public Member Functions

  AutoPtrRef (OtherType *p)

Public Attributes

OtherType *  mPtr

Constructor & Destructor Documentation

AutoPtrRef ( OtherType *  p ) [inline]
: mPtr(p) {}        

Member Data Documentation