C++ smart pointers and classes I've started learning C++ recently and want from what I've gathered smart pointers are the way to go when storing stuff on the free store. I want to make sure I've got the basic hang of C++(11) correctly. I've implemented a very basic vector class (a mathematical vector) and would greatly appreciate if anyone would point out if I'm doing something wrong or that could be done better.

Vector.h

#ifndef VECTOR
#define VECTOR

#include &lt;memory&gt;

class Vector {
private:
    int x;
    int y;
    int z;

public:
    Vector(int x, int y, int z);
    ~Vector();

    std::unique_ptr&lt;Vector&gt; operator + (Vector* vector);

    int GetX() { return x; }
    int GetY() { return y; }
    int GetZ() { return z; }

    void SetX(int x) { this-&gt;x = x; }
    void SetY(int y) { this-&gt;y = y; }
    void SetZ(int z) { this-&gt;z = z; }
};

#endif


Vector.cpp

#include &lt;memory&gt;
#include &lt;iostream&gt;
#include "Vector.h"

Vector::Vector(int x, int y, int z) : x(x), y(y), z(z) {
    std::cout &lt;&lt; "Constructed a Vector instance with elements ( " &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; "," &lt;&lt; z &lt;&lt; " )." &lt;&lt; std::endl;
};

Vector::~Vector() {
    std::cout &lt;&lt; "Destructed a Vector instance with elements ( " &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; "," &lt;&lt; z &lt;&lt; " )." &lt;&lt; std::endl;
}

std::unique_ptr&lt;Vector&gt; Vector::operator + (Vector* vector) {
    std::unique_ptr&lt;Vector&gt; result = std::make_unique&lt;Vector&gt;(0, 0, 0);

    result-&gt;SetX(this-&gt;x + vector-&gt;GetX());
    result-&gt;SetY(this-&gt;y + vector-&gt;GetY());
    result-&gt;SetZ(this-&gt;z + vector-&gt;GetZ());

    return result;
};


Usage

#include &lt;iostream&gt;
#include &lt;memory&gt;
#include "Vector.h"

int main() {
    std::unique_ptr&lt;Vector&gt; v1 = std::make_unique&lt;Vector&gt;(1, 2, 3);
    std::unique_ptr&lt;Vector&gt; v2 = std::make_unique&lt;Vector&gt;(3, 2, 1);
    std::unique_ptr&lt;Vector&gt; vr = *(v1.get()) + v2.get();
}


When using the overloaded operator (+) it looks a bit ugly but it's the way I found that worked when using unique_ptr&lt;T&gt;. One thing I'm doubting if I'm doing "correctly" is returning a unique_ptr&lt;T&gt;. Should I return a value instead and then create a unique_ptr&lt;T&gt; from that instead?
I dislike that your class mixes functionality (a mathematical vector) with memory management. A class should only do one of those. If I prefer shared_ptr I have to rewrite your whole Vector class.

There is actually a thing in C++ that is better than smart pointers, which is value semantics. People rarely screw up the lifetime management of an int, it just works naturally. When you do tricky lifetime management try to mimic the way int does it.

The operator + should be const (as well as GetX, GetY and GetZ). It should also work on Vectors and not only on pointers. The standard way to add Vectors is this:

Vector operator +(const Vector &amp;lhs, const Vector &amp;rhs){
    return Vector(lhs.GetX() + rhs.GetX(), lhs.GetY() + rhs.GetY(), lhs.GetZ() + rhs.GetZ());
}


Note that this is a free function, not a member function of Vector. Prefer free functions, because they work in more cases (like multiplying an int to a Vector) and are thus more consistent.

This allows you to do this:

Vector v1{ 1, 2, 3 };
Vector v2{ 6, 5, 4 };
auto v3 = v1 + v2;


Now, if you do want to support unique_ptr you can do so basically the same way:

std::unique_ptr&lt;Vector&gt; operator +(const std::unique_ptr&lt;Vector&gt; &amp;lhs,
    const std::unique_ptr&lt;Vector&gt; &amp;rhs){
    return std::make_unique&lt;Vector&gt;(*lhs + *rhs); //uses above operator
}


This allows you to do this:

auto v1 = std::make_unique&lt;Vector&gt;(1, 2, 3);
auto v2 = std::make_unique&lt;Vector&gt;(4, 5, 7);
auto v3 = v1 + v2;


All in all you seem to have a pretty good grip on operator overloading and smart pointers already.