Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I implement a smart pointer type for array and support arithmetic operators?
    primarykey
    data
    text
    <p>I heard smart pointers like std::shared_ptr, std::unique_ptr (and in addition array classes like std::array boost::shared_array) but those pointers don't support arithmetic operations.</p> <p>I want a smart pointer type only for array that acts like normal pointers, in other words, it can have ++p, p++, p--, p+n, n+p, *p, p[n], p-> operators etc.</p> <p>so I wrote a class ArrayPtr:</p> <pre><code>template &lt;typename T&gt; class ArrayPtr final { public: typedef size_t size_type; ArrayPtr() noexcept; ArrayPtr(nullptr_t) noexcept; ArrayPtr(T p[], size_type size); ArrayPtr(const ArrayPtr &amp;p); ~ArrayPtr(); ArrayPtr &amp; operator = (const ArrayPtr &amp;p); ArrayPtr &amp; operator += (size_type offset); ArrayPtr &amp; operator -= (size_type offset); T &amp; operator * () const noexcept; // dereference *p T &amp; operator [] (size_type index) const;// dereference p[index] T * operator -&gt; () const noexcept; // dereference p-&gt; ArrayPtr operator + (size_type offset) const; ArrayPtr operator - (size_type offset) const; ArrayPtr &amp; operator ++ (); // prefix ++p ArrayPtr operator ++ (int); // suffix p++ ArrayPtr &amp; operator -- (); // prefix --p ArrayPtr operator -- (int); // suffix p-- inline T * get() const noexcept; inline T * getFirst() const noexcept; inline T * getLast() const noexcept; inline size_type getPos() const noexcept; inline size_type getSize() const noexcept; private: void addPtr(T p[], size_type size); void removePtr(T p[]); private: T *m_ptr; size_type m_pos; size_type m_size; struct PtrCounter { T *ptr; size_type size; size_type count; }; static vector&lt;PtrCounter&gt; sm_ptrCounters; }; </code></pre> <p>It act like shared_ptr, but to make a dynamic array you should do: <code>ArrayPtr p(new int[8], 8)</code> When accessing array elements you can use <code>p[3] = 10;</code> or <code>*(p+4) = 20;</code></p> <p>And you can also have <code>p++</code> and <code>p += 5</code></p> <p>When you try to give an index out of the array's bounds, it throw an OutOfBound exception.</p> <p>I use static member sm_ptrCounters to record the number of pointers that points to the same array.</p> <p>I can't tell the advantage of doing this, I'm just a nerd that want things to be consistent. And I'd like to know why shouldn't I do this (since no c++ library provide this feature).</p> <p>Any suggestion is welcome :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload