Smart but simple pointers For practice, I've implemented my own smart pointer class. It's a 'unique pointer', meaning it doesn't allow copies of itself (when copying from a into b, a is 'emptied' after the copying, i.e. the raw pointer is set to 0), so there can never be more than one time where delete is called on the raw pointer.

It utilizes RAII to bound the memory deallocation to the object lifetime, so it is just a basic smart pointer.

#ifndef SMARTPOINTER_CPP
#define SMARTPOINTER_CPP
#include &lt;iostream&gt;

template &lt;typename T&gt;
class SmartPointer {
public:
    SmartPointer(T* pointer = NULL) : pointer(pointer) { }
    ~SmartPointer(){
        delete pointer;
    }
    SmartPointer(SmartPointer&amp; other){
        *this = other;
    }
    SmartPointer&amp; operator=(SmartPointer&amp; other){
        this-&gt;pointer = other.pointer;
        other.pointer = NULL;

        return *this;
    }
    T&amp; operator*() const {
        return *pointer;
    }
    T* operator-&gt;() const {
        return pointer;
    }
private:
    T* pointer;
    template &lt;typename E&gt;
    friend std::ostream&amp; operator&lt;&lt;
        (std::ostream&amp; stream, const SmartPointer&lt;E&gt;&amp; smartPointer);
};
template &lt;typename E&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, const SmartPointer&lt;E&gt;&amp; smartPointer){
    stream &lt;&lt; smartPointer.pointer;
}
#endif
This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:


There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer&lt;T&gt; sp; shared_ptr&lt;T&gt; ssp = sp.release();.
You forgot to return the stream inside ostream&amp; operator &lt;&lt;. This should be a compilation error.
There is no need for friend. I don't know who made it popular to make the ostream&amp; operator &lt;&lt; a friend but I'd like to give that person a stern look. Without friend this works just fine:

template &lt;typename E&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, const SmartPointer&lt;E&gt;&amp; smartPointer){
    return stream &lt;&lt; &amp;(*smartPointer);
}

Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.
Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.
Your assignment operator causes a memory leak in the following case:

SmartPointer&lt;int&gt; sp1 = new int{1};
SmartPointer&lt;int&gt; sp2 = new int{2};
sp1 = sp2;


The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

SmartPointer&lt;int&gt; sp = new int;
sp = sp;



I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.