#include <FITSUtil.h>
Public Member Functions | |
| auto_array_ptr (X *p=0) throw () | |
| constructor. allows creation of pointer to null, can be modified by reset() | |
| auto_array_ptr (auto_array_ptr< X > &right) throw () | |
| copy constructor | |
| ~auto_array_ptr () | |
| destructor. | |
| void | operator= (auto_array_ptr< X > &right) |
| assignment operator: transfer of ownership semantics | |
| X & | operator * () throw () |
| deference operator | |
| X & | operator[] (size_t i) throw () |
| return a reference to the ith element of the array | |
| X | operator[] (size_t i) const throw () |
| return a copy of the ith element of the array | |
| X * | get () const |
| return a token for the underlying content of *this | |
| X * | release () throw () |
| return underlying content of *this, transferring memory ownership | |
| X * | reset (X *p) throw () |
| change the content of the auto_array_ptr to p | |
Static Public Member Functions | |
| static void | remove (X *&x) |
| utility function to delete the memory owned by x and set it to null. | |
This code was written by Jack Reeves and first appeared C++ Report, March 1996 edition. Although some authors think one shouldn't need such a contrivance, there seems to be a need for it when wrapping C code.
Usage: replace
float* f = new float[200];
with
FITSUtil::auto_array_ptr<float> f(new float[200]);
Then the memory will be managed correctly in the presence of exceptions, and delete will be called automatically for f when leaving scope.
1.5.4