Cross-Platform C++

ot
class ArrayAutoPtr< T >

#include "ot/base/ArrayAutoPtr.h"

A variation of std::auto_ptr which holds a pointer to an array and uses the associated delete [] syntax when it needs to destroy the array. The following example shows how an ArrayAutoPtr is initialized with a new array pointer and manages the memory even in the presence of exceptions:

    // create a scope for the automatic deletion of the array
    {
        ArrayAutoPtr<float> apArray(new float[20]);
        // use operator[] to access the array members
        apArray[0] = 3.142;
        throw Exception(OT_T("test"));
        // array is automatically freed at exit from scope
    }

See also:
std::auto_ptr



Constructor/Destructor Summary
ArrayAutoPtr(T* ptr)
         Creates an ArrayAutoPtr which takes ownership of the array starting at ptr.
ArrayAutoPtr(const ArrayAutoPtr< T >& rhs)
         Copy constructor which takes ownership of the array currently owned by rhs.
~ArrayAutoPtr()
         Destructor which deletes the contained array pointer if it is 'owned' by this object.

Method Summary
 T* get() const
         Returns the contained array pointer.
 T& operator *() const
         Dereference operator which returns a reference to the first element of the contained array.
 T& operator[](size_t n) const
         Dereference operator which returns a reference to the nth element of the contained array.
 ArrayAutoPtr< T >& operator=(const ArrayAutoPtr< T >& rhs)
         Assignment operator which takes ownership of the array currently owned by rhs.
 T* release() const
         Returns the contained pointer and revokes 'ownership', so that the destructor or assignment will not delete the contained pointer.

Typedefs

element_type

typedef T element_type

Constructor/Destructor Detail

ArrayAutoPtr

 ArrayAutoPtr(T* ptr)
Creates an ArrayAutoPtr which takes ownership of the array starting at ptr.


ArrayAutoPtr

 ArrayAutoPtr(const ArrayAutoPtr< T >& rhs)
Copy constructor which takes ownership of the array currently owned by rhs. Note that rhs is modified by this operation even though it is marked as const.


~ArrayAutoPtr

 ~ArrayAutoPtr()
Destructor which deletes the contained array pointer if it is 'owned' by this object.


Method Detail

get

T* get() const
Returns the contained array pointer.


operator *

T& operator *() const
Dereference operator which returns a reference to the first element of the contained array.


operator[]

T& operator[](size_t n) const
Dereference operator which returns a reference to the nth element of the contained array.


operator=

ArrayAutoPtr< T >& operator=(const ArrayAutoPtr< T >& rhs)
Assignment operator which takes ownership of the array currently owned by rhs. If this ArrayAutoPtr already owns an array pointer it is freed.

Note that rhs is modified by this operation even though it is marked as const.


release

T* release() const
Returns the contained pointer and revokes 'ownership', so that the destructor or assignment will not delete the contained pointer.



Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements