#include <DynTmp.hh>
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):
Example:
// 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 }
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. |
|
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. |
|
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. |
|
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. |