Pointer handle - absolute follow-up This is a follow-up to:


Pointer handle - follow-up
Pointer class/handle


Please review my pointer class.

template&lt;typename T&gt;
class Ptr {
public:
    Ptr(T* t, int s = 1) : sz{s&lt;1 ? throw std::logic_error("Invalid sz parameter") : s} {
        sz = s;
        p = new T[sz];
        std::copy(t,t+sz,p);
    }

    Ptr(const Ptr&amp; t) : Ptr(t.p, t.sz) { }

    Ptr&amp; operator=(Ptr copy) {
        std::swap(copy.sz, sz);
        std::swap(copy.p, p);
        return *this;
    }

    Ptr(Ptr &amp;&amp;t) :p{t.p}, sz{t.sz} {
        t.p = nullptr;
        t.sz = 0;
    }

    Ptr&amp; operator=(Ptr &amp;&amp;t) {
        std::swap(t.p,p);
        std::swap(t.sz,sz);
        return *this;
    }

    T&amp; operator*() {
        check_range(index);
        return p[index];
    }

    T&amp; operator[](int i) {
        check_range(i);
        return p[i];
    }

    T* get() const {
        return p;
    }

    void operator+=(int i) {
        check_range(index+i);
        index += i;
    }

    void operator-=(int i) {
        operator+=(-i);
    }

    Ptr operator+(int i) {
        Ptr old{*this};
        old += index+i;
        return old;
    }

    Ptr operator-(int i) {
        return operator+(-i);
    }

    Ptr&amp; operator++() {
        operator+=(1);
        return *this;
    }

    Ptr operator++(int) {
        Ptr old{p+index};
        operator++();
        return old;
    }

    Ptr&amp; operator--() {
        operator-=(1);
        return *this;
    }

    Ptr operator--(int) {
        Ptr&lt;T&gt; old{p+index};
        operator--();
        return old;
    }

    ~Ptr() {
        delete[] p;
    }

private:
    T* p;
    int sz;
    int index = 0;

    void check_range(int i) {
        if (i &lt; 0 || i &gt; sz-1) {
            throw std::out_of_range("out of range");
        }
        if (p+i == nullptr) {
            throw std::out_of_range("null pointer");
        }
    }

};
Ptr(T* t, int s = 1): sz{s&lt;1 ? throw std::logic_error("Invalid sz parameter") : s}
{
    sz = s; //this is unnecessary
    p = new T[sz];
    std::copy(t,t+sz,p);
}


Here you are writing to sz twice, first in the initializer list then in the constructors body. In general it's best if you just write only once in the initializer list if possible:

Ptr(T* t, int s = 1):
    sz{s&lt;1 ? throw std::logic_error("Invalid sz parameter") : s}
{
    p = new T[sz];
    std::copy(t,t+sz,p);
}


In the move constructor:

Ptr(Ptr &amp;&amp;t):
    p{t.p},
    sz{t.sz}
{
    t.p = nullptr;
    t.sz = 0;
}


we are changing the pointer and size in this class but not the index. I would "move" everything and take the index from the other class here as well.
We need to set t.p to nullptr so when t gets destructed the the call to delete[] p; will be a no-op, so that part is necessary. However I don't see why the sz needs to be set to 0 because it's a primitive and therefore not in the destructor. Because the range checking function always checks p for a nullptr we can't index the old object anyway so I think I would just remove that expression.

Ptr(Ptr &amp;&amp;t):
    p{t.p},
    sz{t.sz},
    index{t.index}
{
    t.p = nullptr;
}


(Disclaimer: I'm not especially experienced with c++ move semantics so if you think it's more readable to keep explicitly setting every element to a "null" type of value for readability please make a comment, I'd like to know what people think about that.)