FX::FXPtrHold< type > Class Template Reference
#include <FXPtrHold.h>
List of all members.
Detailed Description
template<class type>
class FX::FXPtrHold< type >
A guaranteed deleted pointer holder.
In code using exception handling, a major problem is allocating pointers on the stack to objects and then correctly freeing them on exception. The solution is this class which guarantees deletion of new-ed pointers stored inside it.
FXPtrHold<FXString> temp;
FXERRHM(temp=new FXString);
Usage is recommended for any new-ed pointer which doesn't inherit
FXObject (as obviously
FXObject manages auto-deletion for its children). Other common uses I have found is fire-and-forget static allocations.
Why use this instead of std::auto_ptr
from the STL? Well auto_ptr
has the concept of ownership of the pointed to data and assignments from one to the other zeros the copyee. FXPtrHold behaves much more like a vanilla auto pointer - the sole difference it that it deletes its contents on destruction.
Needless to say, you should zero its contents if you delete them manually. FXDELETE() helps you here. Remember also that if you do make copies, deleting one means setting the rest to zero to prevent a run-time error.
- Warning:
- Do not think of FXPtrHold as a smart pointer - it's a pointer holder, which is not the same thing. Thus setting a FXPtrHold to some new value does not delete its previous contents. Furthermore, implicit conversions to the held pointer are provided which well-designed smart pointers don't do eg; you can do
delete ptr
.
If you
do want a smart pointer, see
FX::Generic::ptr
|
Public Member Functions |
| FXPtrHold (type *p=0) throw () |
| operator type * () throw () |
| operator const type * () const throw () |
type *& | operator-> () throw () |
type *& | operator= (type *p) throw () |
void | free () |
The documentation for this class was generated from the following file: