Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

NoPtr::DynTmp< ObjType > Class Template Reference

#include <DynTmp.hh>

List of all members.

Detailed Description

template<typename ObjType>
class NoPtr::DynTmp< ObjType >

A DynTmp represents an unnamed temporary DynObj.

It does not provide any method, i.e. once constructed it can only be given to a DynObj or destroyed. If a DynTmp is not "consumed", i.e. acquired by a DynObj, it will destroy the DAO held.

Whenever you find yourself needing to create an unnamed temporary from a DynObj, such as for returning a DynObj from a function, you must use DynTmp instead. This is because DynObj is protected from implicit copying. This protection is provided to avoid two sources of bugs, found in some smart-pointer classes (std::auto_ptr and boost::shared_ptr):


    // if no one accepts the return value, the 
    // int is automatically destroyed
    DynTmp<int> getInt()
        return new int(123);
    DynTmp<int> getAnotherInt()
        DynObj<int> someInt;
        // do stuff with it, then return it: 
        return someInt.moveToTmp();
        // return someInt; won't work because not allowed
    DynObj<int> getBadInt(); // not allowed for safety reasons
    int main()
        getInt(); // no leak: int destroyed by DynTmp
        DynObj<const int> anInt( getInt() );
        assert(anInt() == 123);
        // anInt() = 456; // compile error: non-const operation on const int

Oliver Schoenborn
Apr 2003

Definition at line 32 of file DynTmp.hh.

Public Member Functions

 DynTmp (const DynTmp &dynmobj) throw ()
 Create from another unnamed temporary. The dynmobj is empty after this construction.

template<typename ObjType2>  DynTmp (const DynTmp< ObjType2 > &dynmobj) throw ()
 Create from another unnamed temporary of different type.

 ~DynTmp ()
 Destroy the temporary.

 DynTmp (ObjType *ptr) throw ()
 Create from a DAO. Takes ownership of the DAO.

ObjType * release () throw ()
 Release the DAO that we own.

Constructor & Destructor Documentation

template<typename ObjType>
template<typename ObjType2>
NoPtr::DynTmp< ObjType >::DynTmp const DynTmp< ObjType2 > &  dynmobj  )  throw () [inline]

Create from another unnamed temporary of different type.

This will compile only if the conversion from ObjType to ObjType2 is valid. The dynmobj is empty after this construction.

Definition at line 51 of file DynTmp.hh.

References NoPtr::NullPtr.

template<typename ObjType>
NoPtr::DynTmp< ObjType >::~DynTmp  )  [inline]

Destroy the temporary.

This destroys the DAO contained, if any, and notifies any RRefs that were linked to the original DynObj (if any) that the DAO has been destroyed.

Definition at line 61 of file DynTmp.hh.

Member Function Documentation

template<typename ObjType>
ObjType* NoPtr::DynTmp< ObjType >::release  )  throw () [inline]

Release the DAO that we own.

If we are null (i.e. not owning anything), returns NULL. The caller is responsible for calling delete on the pointer received.

Definition at line 74 of file DynTmp.hh.

References NoPtr::NullPtr.

The documentation for this class was generated from the following file:
Generated on Mon Aug 4 18:51:33 2003 for NoPtr C++ Library by doxygen 1.3.2