1

I'm writing my own vector class (a container for x, y values) and I'm not really sure what constructors/assignment operators I should implement on my own and what I can count on the compiler to provide. Obviously, I need to write any method that doesn't have the default behaviour or isn't auto-generated in any case. But sure enough, there's also no point in implementing something if the compiler can generate the exactly same thing.

I'm using Visual Studio 2010 (which might matter in the aspect of C++11). Also my class is a template one, if that matters.

Currently, I have:

template <typename T>
class Integral2
{
    // ...

    Integral2(void)
        : x(0), y(0)
    {}

    Integral2(Integral2 && other)
        : x(std::move(other.x)), y(std::move(other.y))
    {}

    Integral2(T _x, T _y)
        : x(_x), y(_y)
    {}

    Integral2 & operator =(Integral2 const & other)
    {
        x = other.x;
        y = other.y;
        return *this;
    }

    Integral2 & operator =(Integral2 && other)
    {
        x = std::move(other.x);
        y = std::move(other.y);
        return *this;
    }

    // ...
};

Do I need copy ctor/assignment operator when I have a move ctor/move operator?

4

1 回答 1

1

In C++, there is the Rule of Three which provides guidelines for which constructors/operators you should define based on which you are sure you need. In C++11, some believe that move semantics push the rule of three to become the rule of five. Look at this thread for an example implementation. I recommend you look into these guidelines.

于 2013-03-31T00:54:42.000 回答